如何创建 SQL 查询以连接和解析结果集中的值
How to create SQL Query that concatenates and parses value in result set
如何根据以下条件对 table 进行 SQL 查询:
- 结果是一个单列,它将所有由破折号分隔的字段连接成一个字符串(例如:FieldA-FieldB-FieldC-FieldD-FieldE)
- 如果给定的字段为 NULL 或者字段的值是一个字符串,例如“EMPTY”或“NA”,执行不将该字段的值连接到结果字符串中
示例 Table 人物(名字、姓氏、街道、城市、州):
Bob | Dylan | 555 Street | Mountain View | California
Ally | M | NULL | Seattle | Washington
Jan | Van | EMPTY | EMPTY | Oregon
Nancy | Finn | EMPTY | EMPTY | NA
Don | William | NULL | EMPTY | Illinois
结果:
Bob-Dylan-555 Street-Mountain View-California
Ally-M-Seattle-Washington
Jan-Van-Oregon
Nancy-Finn
Don-William-Illinois
我知道这可以通过编程方式完成,但想知道这是否可以在 SQL 中完成,以及在查询本身中这样做是否更有效。
你的 select 应该是这样的:
select isnull(FieldA,'')+ '-' + isnull (FieldB,'') + '-' + isnull (FieldC,'') ....
等等..
如果您不希望在 MS SQL 服务器上工作,如果前一个字段为空而不是您应该使用 case 语句。
如果您还想替换 'Empty' 或 'NULL' 字符串,您应该使用:
select replace(replace( isnull(FieldA+'-','') , 'Empty' , ''),'Null', '')
我通过 Nitin_g3 观察修改了 isnull()。
首先,使用 CONCAT 连接字段。
然后使用 REPLACE 替换 NULL 值
SELECT REPLACE( CONCAT( field1, "-", field2 , "-", field3) , "NULL", "EMPTY" )
FROM `table`
试试这个
SELECT ISNULL(FirstName,'') + '-' +
ISNULL (LastName,'') + '-' +
ISNULL (City,'') + '-' +
ISNULL (State,'')
FROM Person
或者像这样
SELECT CASE WHEN ISNULL(FirstName,'') = '' THEN '' ELSE FirstName + '-' +
CASE WHEN ISNULL(LastName,'') = '' THEN '' ELSE LastName + '-' +
CASE WHEN ISNULL(City,'') = '' THEN '' ELSE City + '-' +
CASE WHEN ISNULL(State,'') = '' THEN '' ELSE State + '-' END AS
ColumnName
FROM Person
SQL Server 2017 及更高版本的完整解决方案:
SELECT *
FROM Person p
OUTER APPLY (
SELECT STRING_AGG(NULLIF(NULLIF(val, 'EMPTY'), 'NA'), '-')
WITHIN GROUP (ORDER BY n) AS val
FROM (VALUES (1, p.FirstName), (2, p.LastName),(3, p.Street),
(4,p.City), (5, p.State)) z(n, val)
)sub;
MySQL 版本使用 CONCAT_WS
:
CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
SELECT CONCAT_WS('-',
NULLIF(NULLIF(FirstName, 'EMPTY'), 'NA'),
NULLIF(NULLIF(LastName, 'EMPTY'), 'NA'),
NULLIF(NULLIF(Street, 'EMPTY'), 'NA'),
NULLIF(NULLIF(City, 'EMPTY'), 'NA'),
NULLIF(NULLIF(State, 'EMPTY'), 'NA')) AS r
FROM Person p;
如何根据以下条件对 table 进行 SQL 查询:
- 结果是一个单列,它将所有由破折号分隔的字段连接成一个字符串(例如:FieldA-FieldB-FieldC-FieldD-FieldE)
- 如果给定的字段为 NULL 或者字段的值是一个字符串,例如“EMPTY”或“NA”,执行不将该字段的值连接到结果字符串中
示例 Table 人物(名字、姓氏、街道、城市、州):
Bob | Dylan | 555 Street | Mountain View | California
Ally | M | NULL | Seattle | Washington
Jan | Van | EMPTY | EMPTY | Oregon
Nancy | Finn | EMPTY | EMPTY | NA
Don | William | NULL | EMPTY | Illinois
结果:
Bob-Dylan-555 Street-Mountain View-California
Ally-M-Seattle-Washington
Jan-Van-Oregon
Nancy-Finn
Don-William-Illinois
我知道这可以通过编程方式完成,但想知道这是否可以在 SQL 中完成,以及在查询本身中这样做是否更有效。
你的 select 应该是这样的:
select isnull(FieldA,'')+ '-' + isnull (FieldB,'') + '-' + isnull (FieldC,'') ....
等等..
如果您不希望在 MS SQL 服务器上工作,如果前一个字段为空而不是您应该使用 case 语句。
如果您还想替换 'Empty' 或 'NULL' 字符串,您应该使用:
select replace(replace( isnull(FieldA+'-','') , 'Empty' , ''),'Null', '')
我通过 Nitin_g3 观察修改了 isnull()。
首先,使用 CONCAT 连接字段。 然后使用 REPLACE 替换 NULL 值
SELECT REPLACE( CONCAT( field1, "-", field2 , "-", field3) , "NULL", "EMPTY" )
FROM `table`
试试这个
SELECT ISNULL(FirstName,'') + '-' +
ISNULL (LastName,'') + '-' +
ISNULL (City,'') + '-' +
ISNULL (State,'')
FROM Person
或者像这样
SELECT CASE WHEN ISNULL(FirstName,'') = '' THEN '' ELSE FirstName + '-' +
CASE WHEN ISNULL(LastName,'') = '' THEN '' ELSE LastName + '-' +
CASE WHEN ISNULL(City,'') = '' THEN '' ELSE City + '-' +
CASE WHEN ISNULL(State,'') = '' THEN '' ELSE State + '-' END AS
ColumnName
FROM Person
SQL Server 2017 及更高版本的完整解决方案:
SELECT *
FROM Person p
OUTER APPLY (
SELECT STRING_AGG(NULLIF(NULLIF(val, 'EMPTY'), 'NA'), '-')
WITHIN GROUP (ORDER BY n) AS val
FROM (VALUES (1, p.FirstName), (2, p.LastName),(3, p.Street),
(4,p.City), (5, p.State)) z(n, val)
)sub;
MySQL 版本使用 CONCAT_WS
:
CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
SELECT CONCAT_WS('-',
NULLIF(NULLIF(FirstName, 'EMPTY'), 'NA'),
NULLIF(NULLIF(LastName, 'EMPTY'), 'NA'),
NULLIF(NULLIF(Street, 'EMPTY'), 'NA'),
NULLIF(NULLIF(City, 'EMPTY'), 'NA'),
NULLIF(NULLIF(State, 'EMPTY'), 'NA')) AS r
FROM Person p;