忽略导出的 .csv 文件中的空值,bcp 实用程序

Ignore null's in exported .csv file, bcp utility

How can I ignore null's in my exported .csv file, I used bcp utility within sql server, here it is how this looks like altogether.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ('[dbo].[generateCSV]') IS NOT NULL
DROP PROCEDURE [dbo].[generateCSV]
GO

  CREATE PROCEDURE [dbo].[generateCSV]

(
 @table varchar(100),
 @output varchar(100), 
 @date varchar(12),
 @server varchar(30)
)

AS

DECLARE @sql varchar(8000)

SELECT @sql = 'bcp "select * from ' + DB_NAME() + '.dbo.' + @table  + ' 
where reportingdate = ''' + @date + '''"' + ' queryout ' +  @output + '  -c 
-C65001 -t";" -r"\n" -T -S' + @server 

 exec master..xp_cmdshell @sql 




       -- Main EXEC

EXEC dbo.generateCSV @table = 'Clients', @date = '2017-10-31', @output = 
'//172.18.16.109/share/Test.csv (server with export target location ) ', 
@server = '172.18.16.108(server we are connected to and from which we are 
taking the data)' 

在记事本++中导出并打开我的文件后,空列最终填充了 NULL。

我希望它看起来像...


编辑:

我在我的 select 子句中包含 ISNULL() 但记事本仍然会将这些列读取为空值。

SELECT @sql = 'bcp "select 
ReportingDate,uniqClientID,registrationNumber,name,ISNULL(vatNumber,'''') as 
vatNumber,ISNULL(entityStatusCode,'''') as entityStatusCode, 
maximumLifetimeDPD from ' + DB_NAME() + '.dbo.' + 
@table  + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +  
@output + '  -c -C65001 -t";" -r"\n" -T -S' + @server 

SELECT @sql = 'bcp "select * from ' + DB_NAME() + '.dbo.' + @table + ' where reportingdate = ''' + @date + '''"' + ' queryout ' + @output + ' -c -C65001 -t";" -r"\n" -T -S' + @server

而不是 SELECT * 写具体的列名。

哪里觉得NULL可以出现就可以像下面这样写

....   
SELECT 
Column1,
ISNULL(Column2,'') AS Column2,
Column3,
....
 from ' + DB_NAME() + '.dbo.' + @table 
.....

注意:如果您使用 INT 作为数据类型,则需要根据以下查询稍微更改 select。

DECLARE @INTVAL INT
SET @INTVAL = NULL
SELECT
ISNULL(LTRIM(NULLIF(@INTVAL, 0)), '') AS ColumnName