title: BCP outputs 'NULL' after running the query
body: <p>I've been searching for the answer to this problem and could not find anything so far.</p>
  
  <p>When executing bcp command with a query, I'm getting <code>NULL</code> as an output.</p>
  
  <p>However, when executing the query by itself, everything looks good. </p>
  
  <p>First, I thought, that may be my query has errors, but after it executed successfully, I got more confused.</p>
  
  <p>So, here is my code executing bcp command:</p>
  
  <pre><code>DECLARE @query VARCHAR(2000)
  DECLARE @bcpCommand VARCHAR(1024)
  DECLARE @sharedDevFolder VARCHAR(500)
  DECLARE @fileName VARCHAR(200)
  DECLARE @environment VARCHAR(5)
  DECLARE @customerCode VARCHAR(5)
  DECLARE @parserConfig VARCHAR(5)
  DECLARE @bucketAssign VARCHAR(10)
  DECLARE @dateFormat VARCHAR(15)
  DECLARE @input INT
  DECLARE @RC INT
  
  SET @sharedDevFolder = '\\REMOTESERVER\Data'
  SET @input = 201507
  SET @fileName = 'Transaction-' + 
                  @environment + '-' + 
                  @customerCode + '-' + 
                  @parserConfig + '-' + 
                  @bucketAssign + '-' + 
                  @dateFormat + '.txt'
  
  SET @query = 'SELECT ''Transaction Unique ID'', ''Transaction Date'', ''Person Unique ID'' UNION ALL SELECT NULL AS ''Transaction Unique ID'', CONVERT(VARCHAR(10),CONVERT(DATETIME,Date,1),101) AS ''Transaction Date'', NULL AS ''Person Unique ID'' FROM tblChromeRiverInitData WHERE YYYYMM = ' + CAST(@input as VARCHAR)
  
  select @query
  
  --need to change @sharedDevFolder depending on a server environment
  SET @bcpCommand = 'bcp "' + @query + '" queryout "'
  set @bcpCommand = @bcpCommand + @sharedDevFolder + @fileName + '" -c -T -t^| -r\n'
  
  EXEC @RC = master..xp_cmdshell @bcpCommand
  </code></pre>
  
  <p>And here is my query by itself:</p>
  
  <pre><code>DECLARE @input INT
  SET @input = 201507
  SELECT 'Transaction Unique ID','Transaction Date', 'Person Unique ID' UNION ALL SELECT NULL AS 'Transaction Unique ID', CONVERT(VARCHAR(10),CONVERT(DATETIME,Date,1),101) AS 'Transaction Date', NULL AS 'Person Unique ID' FROM tblChromeRiverInitData WHERE YYYYMM = @input
  </code></pre>
  
  <p>This query executes fine.</p>
  
  <p>I'm not sure where to look from now on.</p>