0

我有以下 SQL 查询(SQL Server 2005):

Select TOP 1000 col1,
LTRIM(RTRIM(cast(col2 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col3 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col4 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col5 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col6 as varchar))))
from mytable

但是我遇到了一些问题,因为这些列都是可以为空的,并且在这个连接中我不能有 NULL 值。在这种情况下避免 NULL 值的最佳方法是什么?

提前致谢!

4

4 回答 4

3

Wrap the fields in ISNULL()

Select TOP 1000 col1,
LTRIM(RTRIM(cast(ISNULL(col2,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col3,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col4,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col5,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col6,'') as varchar))))
from mytable

Or COALESCE()

Select TOP 1000 col1,
LTRIM(RTRIM(cast(COALESCE(col2,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col3,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col4,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col5,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col6,'') as varchar))))
from mytable

If using SQL Server 2012 it's even easier thanks to CONCAT() (you can replace SPACE(2) with ' | ' above too):

Select TOP 1000 col1
          ,CONCAT(LTRIM(RTRIM(cast(col2 as varchar)))
                ,' | ', LTRIM(RTRIM(col3)) 
                ,' | ', LTRIM(RTRIM(col4)) 
                ,' | ', LTRIM(RTRIM(col5)) 
                ,' | ', LTRIM(RTRIM(col6)))
from mytable
于 2013-09-16T13:56:09.967 回答
1

You can have some other value when there is a NUll, through ISNULL function. Here is a sample query:

Select TOP 1000 col1,
LTRIM(RTRIM(cast(ISNULL(col2,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col3,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col4,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col5,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col6,'SOMEVALUE') as varchar))))
from mytable
于 2013-09-16T13:55:35.900 回答
1

如果这是一个非关键系统,并且您不必为每列 COALESCE 或 ISNULL 烦恼,您可以使用 CONCAT_NULL_YIELDS_NULL 服务器变量,它将连接的空值视为空字符串:

SELECT 'Hi ' + NULL

将产生空值。

SET CONCAT_NULL_YIELDS_NULL OFF

SELECT 'Hi' + NULL

SET CONCAT_NULL_YIELDS_NULL ON

产生:“嗨”。

文档在这里- 请注意,他们将在未来删除此功能。

否则,只需关闭设置,然后运行您的原始查询。

http://technet.microsoft.com/en-us/library/ms176056.aspx

于 2013-09-16T13:57:42.190 回答
0

如果您想正确连接您的值并且不想为空值使用一些占位符,请使用isnull(或coalesce)+ stuff

select top 1000
    col1,
    stuff(
       isnull('  |  ' + cast(col2 as varchar), '') +
       isnull('  |  ' + cast(col3 as varchar), '') +
       isnull('  |  ' + cast(col4 as varchar), '') +
       isnull('  |  ' + cast(col5 as varchar), '')
    , 1, 3, '')
from mytable

sql fiddle demo

于 2013-09-16T14:00:52.770 回答