SQL Server列断行

时间:2016-08-19 11:41:35

标签: sql-server sql-server-2008-r2

我在SQL查询中有以下行我想在BookedByOrderNo之前添加换行符,不知道怎么做?

,FromAddress=  LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(( case when (b.FromDoorNo is not NULL AND  b.FromDoorNo!='') then b.FromDoorNo + ' - '+ b.FromAddress else b.FromAddress END),'LONDON',''),'ROAD','RD'),'DRIVE','DR'),'AVENUE','AVE'),'GREENFORD',''),'LANE','LN')))

,ToAddress=LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(( case when (b.ToDoorNo is not NULL and b.ToDoorNo!='') then b.ToDoorNo + ' - '+ b.ToAddress else b.ToAddress END),'LONDON',''),'ROAD','RD'),'DRIVE','DR'),'AVENUE','AVE'),'GREENFORD',''),'LANE','LN')))

我建议的查询我在下面输入,但是它的显示结果如下,并且地址显示为null;(如果由id null表示不显示预订,如果orderno null不显示orderno,但总是显示地址;(我的错误在哪里)查询?

  ,FromAddress=  LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(( case when (b.FromDoorNo is not NULL AND  b.FromDoorNo!='') then b.FromDoorNo + ' - '+ b.FromAddress else b.FromAddress END),'LONDON',''),'ROAD','RD'),'DRIVE','DR'),'AVENUE','AVE'),'GREENFORD',''),'LANE','LN')))+ CHAR(13) +(CASE WHEN  b.BookedBy IS NOT NULL THEN 'BookedBy: ' +b.BookedBy else null end) + CHAR(13) +(CASE WHEN  b.OrderNo IS NOT NULL THEN 'OrderNo: ' + b.OrderNo else null end) 

enter image description here

1 个答案:

答案 0 :(得分:1)

添加 CHAR(13),以添加新行/换行符。

DECLARE @Variable VARCHAR(500)='AB-ASDA233 - HEATHROW TERMINAL 4, TW6 2GA BookedBy: adeelcheck OrderNo: adeelorder'

SELECT REPLACE(REPLACE(@Variable,'OrderNo:',CHAR(13) + 'OrderNo:'),'BookedBy:',CHAR(13) + 'BookedBy:') 

最终结果将是:

 AB-ASDA233 - HEATHROW TERMINAL 4, TW6 2GA 
 BookedBy: adeelcheck 
 OrderNo: adeelorder