我正在尝试将地址连接到表格中的单个字段。
Create table temp(ID int, Address1 varchar(100),Address2 varchar(100),Address3 varchar(100),City varchar(100), State varchar(100))
Insert into temp(1,'2 Main St','','','Allentown','PA')
Insert into temp(2,'4 Bee St','PO Box 5067','','Bethlehem','PA')
Insert into temp(3,'Building Two','Suite 208, 707 Alexander Road,','Pembroke Park','Easton','PA')
我想创建一个显示ID,Address作为一个字段的视图。即
2 Main St, Allentown,PA
4 Bee St,PO Box 5067,Bethlehem,PA
Building Two,Suite 208, 707 Alexander Road,Pembroke Park,Easton,PA
Create VIEW [dbo].[vwtemp]
AS
SELECT ID,
isnull(Address1,NULL) AS "Address1",
isnull(Address2,NULL) AS "Address2",
isnull(Address3,NULL) AS "Address3",
isnull(City,NULL) AS "City",
isnull(State,NULL) AS "State",
Stuff(
Coalesce(', ' + [Address1], '')
+ Coalesce(', ' + [Address2],'')
+ Coalesce(', ' + [Address3], '')
+ Coalesce(', ' + [City], '')
+ Coalesce(', ' + [State], '')
) from temp
以上查询在ID 1时插入额外的逗号,因为Address3& 2为空; ID = 2 Address3为空;
任何建议。
由于 [R
答案 0 :(得分:1)
USE CASE语句连接字符串CASE WHEN(Address2 IS NULL)那么'' ELSE(',' +地址2)END