I have the following SQL server query:
我有以下SQL服务器查询:
SELECT COL.FullName AS 'Name',
COB.Reference AS 'Reference',
COL.WordProcessorText AS 'Template Text',
CASE COB.Available
WHEN 1 THEN 'TRUE'
WHEN 0 THEN 'FALSE'
END AS 'Available For Selection',
COL.HelpText AS 'Help Text',
CASE COB.DisplayAlert
WHEN 1 THEN 'TRUE'
WHEN 0 THEN 'FALSE'
END AS 'Display Alert',
COL.AlertMessage AS 'Alert Text',
COB.ParentIdLevel1 AS 'Parent Option',
COB.ParentIdLevel2 AS 'Parent Option 1',
COB.ParentIdLevel3 AS 'Parent Option 2',
COB.ParentIdLevel4 AS 'Parent Option 3',
FL.Name AS 'Category Name',
EL.SingularText AS 'Entity'
FROM dbo.rCategoryOptionLiteral AS COL
INNER JOIN dbo.CategoryOptionBase AS COB ON COB.Id = COL.ObjectId AND COL.Locale = @Locale
INNER JOIN dbo.FieldLiteral AS FL ON FL.ObjectId = COB.FieldId AND FL.Locale = @Locale
INNER JOIN dbo.FieldBase AS FB ON FB.Id = FL.ObjectId
INNER JOIN dbo.EntityBase AS EB ON EB.Id = FB.EntityId
INNER JOIN dbo.EntityLiteral AS EL ON EL.ObjectId = EB.Id AND EL.Locale = @Locale
WHERE COB.FieldId = @FieldId
AND COB.ParentOptionId IS NULL;
My issue is that the data in the four ParentIdLevel
columns can be NULL
and essentially I need to display the NULL
s as blanks in the output. However, as the ParentIdLevel
columns are of type unqiueidentifier in my CategoryOptionBase
table, I am having difficulty displaying these as a blank if they are NULL
. I have tried the COALESE
function but this hasn't worked for me.
我的问题是四个ParentIdLevel列中的数据可以为NULL,实际上我需要在输出中将NULL显示为空白。但是,由于ParentIdLevel列在我的CategoryOptionBase表中的类型为unqiueidentifier,如果它们为NULL,我很难将它们显示为空白。我尝试过COALESE功能,但这对我没用。
2 个解决方案
#1
3
You have to convert uniqueidentifier to string type.
您必须将uniqueidentifier转换为字符串类型。
isnull(convert(char(36),COB.ParentIdLevel1),'') as 'Parent Option',
isnull(convert(char(36),COB.ParentIdLevel2),'') as 'Parent Option 1',
isnull(convert(char(36),COB.ParentIdLevel3),'') as 'Parent Option 2',
isnull(convert(char(36),COB.ParentIdLevel4),'') as 'Parent Option 3',
#2
1
The replacement value needs to be of the same datatype as the column. If you want to return empty strings for null
values for a non-character datatype, you can convert the column to a character datatype inside the function.
替换值需要与列的数据类型相同。如果要为非字符数据类型返回空字符串以获取空值,则可以将该列转换为函数内的字符数据类型。
You can use isnull()
or coalesce()
in sql server.
您可以在sql server中使用isnull()或coalesce()。
select isnull(convert(varchar(36),col),'') as ...
This will return an empty string if col
is null.
如果col为null,则返回空字符串。
or
要么
select coalesce(convert(varchar(36),col),'') as ...
This will also return an empty string if col
is null.
如果col为null,这也将返回一个空字符串。
The main difference between the two is that coalesce()
can support more than 2 parameters, and it selects the first one that is not null
. More differences between the two are answered here.
两者之间的主要区别在于coalesce()可以支持2个以上的参数,并且它选择第一个不为null的参数。这里回答了两者之间的更多差异。
select coalesce(col,col2,'')
coalesce()
is also standard ANSI sql, so it is available in most RDBMS, whereas isnull()
is specific to sql server.
coalesce()也是标准ANSI sql,因此它在大多数RDBMS中都可用,而isnull()特定于sql server。
Reference:
参考:
isnull()
- msdn- isnull() - msdn
coalesce()
- msdn- coalesce() - msdn
coalesce
vsisnull
- Itzik Ben-Gan- coalesce vs isnull - Itzik Ben-Gan
#1
3
You have to convert uniqueidentifier to string type.
您必须将uniqueidentifier转换为字符串类型。
isnull(convert(char(36),COB.ParentIdLevel1),'') as 'Parent Option',
isnull(convert(char(36),COB.ParentIdLevel2),'') as 'Parent Option 1',
isnull(convert(char(36),COB.ParentIdLevel3),'') as 'Parent Option 2',
isnull(convert(char(36),COB.ParentIdLevel4),'') as 'Parent Option 3',
#2
1
The replacement value needs to be of the same datatype as the column. If you want to return empty strings for null
values for a non-character datatype, you can convert the column to a character datatype inside the function.
替换值需要与列的数据类型相同。如果要为非字符数据类型返回空字符串以获取空值,则可以将该列转换为函数内的字符数据类型。
You can use isnull()
or coalesce()
in sql server.
您可以在sql server中使用isnull()或coalesce()。
select isnull(convert(varchar(36),col),'') as ...
This will return an empty string if col
is null.
如果col为null,则返回空字符串。
or
要么
select coalesce(convert(varchar(36),col),'') as ...
This will also return an empty string if col
is null.
如果col为null,这也将返回一个空字符串。
The main difference between the two is that coalesce()
can support more than 2 parameters, and it selects the first one that is not null
. More differences between the two are answered here.
两者之间的主要区别在于coalesce()可以支持2个以上的参数,并且它选择第一个不为null的参数。这里回答了两者之间的更多差异。
select coalesce(col,col2,'')
coalesce()
is also standard ANSI sql, so it is available in most RDBMS, whereas isnull()
is specific to sql server.
coalesce()也是标准ANSI sql,因此它在大多数RDBMS中都可用,而isnull()特定于sql server。
Reference:
参考:
isnull()
- msdn- isnull() - msdn
coalesce()
- msdn- coalesce() - msdn
coalesce
vsisnull
- Itzik Ben-Gan- coalesce vs isnull - Itzik Ben-Gan