Sybase SQL - null条件,删除空格

时间:2015-06-18 04:28:47

标签: sql sybase

我在下面遇到这个问题让我很生气,你能不能修改下面的代码,结果就是这样:" 123 Bridge St 2500"而不是" 123 Bridge St 2500" (开头和St和2500之间的额外空格)?

declare @street varchar(20), @street2 varchar(20), @suburb varchar(20), @postcode varchar(4)
set @street = null
set @street2 = '123 Bridge St'
set @suburb = null
set @postcode = '2500'

select address = case @street when null then '' else @street + ' ' end
                        +case @street2 when null then '' else @street2 + ' ' end
                        +case @suburb when null then '' else @suburb + ' ' end
                        +case @postcode when null then '' else @postcode + ' ' end

-- Expected Result: '123 Bridge St 2500'
-- Actual Result: ' 123 Bridge St  2500'

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试

select case when @street is null then '' else @street + ' ' end
                        +case when @street2 is null then '' else @street2 + ' ' end
                        +case when @suburb is null then '' else @suburb + ' ' end
                        +case when @postcode is null then '' else @postcode + ' ' end

如果您写的case @street when null..when @street = null相同,但不起作用,请改用is null

答案 1 :(得分:0)

感谢您的所有评论,

这里有解决方案

select address = ltrim(rtrim(@street) + ' ') + ltrim(rtrim(@street2) + ' ') + ltrim(rtrim(@suburb) + ' ') + @postcode