努力加入两个文本字段
,RTRIM(LTRIM(REPLACE(IM.ITEMDESC,',',''))) AS ITEMDESCR
,RTRIM(LTRIM(ISNULL(REPLACE(REPLACE(convert(varchar(MAX),[TXTFIELD]),
CHAR(13),' '),',',''),''))) AS EXTRADESCRIPTION
必须删除第二行的回车 - 尝试过 Concat 和 + 但都没有工作。第二行也有空值,因此如果空值只想显示第一行。
努力加入两个文本字段
,RTRIM(LTRIM(REPLACE(IM.ITEMDESC,',',''))) AS ITEMDESCR
,RTRIM(LTRIM(ISNULL(REPLACE(REPLACE(convert(varchar(MAX),[TXTFIELD]),
CHAR(13),' '),',',''),''))) AS EXTRADESCRIPTION
必须删除第二行的回车 - 尝试过 Concat 和 + 但都没有工作。第二行也有空值,因此如果空值只想显示第一行。
create table im (id int not null identity(1,1), itemdesc varchar(64), txtfield text)
insert into im values
('Pencil','Writing utensil')
,('Pen',null)
,(null,'Writing utensil')
select
rtrim(ltrim(replace(im.itemdesc,',',''))) as itemdescr
, rtrim(ltrim(isnull(replace(replace(convert(varchar(max),txtfield), char(13),' '),',',''),''))) as extradescription
, isnull(rtrim(ltrim(replace(im.itemdesc,',','')))+' ','')
+ isnull(rtrim(ltrim(replace(replace(convert(varchar(max),txtfield), char(13),' '),',',''))),'')
as combined_description
from im
rextester 演示:http ://rextester.com/YQDARN44856
返回:
+-----------+------------------+------------------------+
| itemdescr | extradescription | combined_description |
+-----------+------------------+------------------------+
| Pencil | Writing utensil | Pencil Writing utensil |
| Pen | | Pen |
| NULL | Writing utensil | Writing utensil |
+-----------+------------------+------------------------+