我想构建一个查询来连接数据库中的multipla值。 所以这是查询:
SELECT FiscalCode + ' - ' + Name + ' ' + Surname + ' - ' + City + ' - ' BirthTime AS Value, FiscalCode AS OrderBy
FROM AA_V_PHR_CCD_Person
FiscalCode,Name,Surname IS NOT NULL为数据库配置,但City和BirthTime在数据库配置中为NULLABLE。
现在,如果数据库中的City或BirthTime为NULL,查询结果为NULL,我可以解决此问题吗?
答案 0 :(得分:2)
使用coalesce
将null
替换为空字符串。
SELECT FiscalCode + ' - ' + Name + ' ' + Surname + ' - ' + COALESCE(City,'') + ' - '
COALESCE(BirthTime,'') AS Value, FiscalCode AS OrderBy
FROM AA_V_PHR_CCD_Person
从SQL Server 2012及更高版本开始,您可以使用CONCAT
隐式将null
值转换为空字符串。
SELECT CONCAT(FiscalCode , ' - ' , Name , ' ' , Surname , ' - ' , City, ' - ', BirthTime) as Value,
FiscalCode AS OrderBy
FROM AA_V_PHR_CCD_Person
答案 1 :(得分:0)
您可以使用ISNULL
SELECT FiscalCode + ' - ' + Name + ' ' + Surname + ' - ' + ISNULL(City,'') + ' - '
ISNULL(BirthTime,'') AS Value, FiscalCode AS OrderBy
FROM AA_V_PHR_CCD_Person
答案 2 :(得分:0)
你可以使用ISNULL:
SELECT ISNULL(FiscalCode,'') + ' - ' + ISNULL(Name,'') + ' ' + ISNULL(Surname,'') + ' - ' + ISNULL(City,'') + ' - ' + ISNULL(BirthTime,'') AS Value, FiscalCode AS OrderBy FROM AA_V_PHR_CCD_Person