Sybase SQL - 空条件,删除空格
Sybase SQL - null condition, removing whitespace
下面的查询让我很生气,你能不能修复下面的代码,使结果像这样:“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'
谢谢!
尝试
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
。
感谢您的所有评论,
这是有效的解决方案
select address = ltrim(rtrim(@street) + ' ') + ltrim(rtrim(@street2) + ' ') + ltrim(rtrim(@suburb) + ' ') + @postcode
下面的查询让我很生气,你能不能修复下面的代码,使结果像这样:“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'
谢谢!
尝试
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
。
感谢您的所有评论,
这是有效的解决方案
select address = ltrim(rtrim(@street) + ' ') + ltrim(rtrim(@street2) + ' ') + ltrim(rtrim(@suburb) + ' ') + @postcode