English 中文(简体)
TSQL Comma 同一行的有限田
原标题:TSQL Comma Delimited fields on same row
  • 时间:2012-04-18 06:23:21
  •  标签:
  • t-sql

Imagine you have a User table with FirstName, MiddleName, LastName.
For the sake of this sample only FirstName is mandatory.

在NOT失去价值时,最快的办法是将这些3个领域集中起来,并将其与“、”分开,这样:

If only FirstName is NOT null, we should get "John" as result.
If only FirstName and LastName are not null, we should have "John,Wayne" as result.
If FirstName, MiddleName and LastName are not null, we should have "John,M,Wayne" as result.

先进。

最佳回答

或许可以这样说:

FirstName +(CASE WHEN MiddleName IS NULL THEN    ELSE  , +MiddleName END)
+(CASE WHEN LastName IS NULL THEN    ELSE  , +LastName END) 
问题回答

2种不同的方法

--syntax
select Firstname + isnull( ,  +Middlename,   ) + coalesce( ,  +LastName,   )
from 
--testdata
(select  John  firstname,  W  middlename,  Wayne  lastname
union all select  John  firstname, null middlename,  Wayne  lastname
union all select  John  firstname,  W  middlename, null lastname) a




相关问题
两个表格的精度

我正试图加入在服务器上的多个桌子,并保持快速业绩。

SELECT last row if USER is found

I have a log table in SQL Server. Table is structured this way: Unique ProblemID ResponsibleID AssignedToID ProblemCode 155 155 0282 4 156 155 ...

How to convert a Tsql scalar function into a table function?

I am using SSMS 2008 and I have the following scalar function to take a text string and remove all metatags from Microsoft Word. The tags are enclosed in "<...>" and there can be any number of ...

Interesting SQL query

I have this data in my table: onum amt odate cnum snum 3001 18,69 1990-03-10 00:00:00.000 2008 1007 3002 1900,10 1990-03-10 00:00:00.000 ...

热门标签