Sql server 基于联接两个表的SQL Server视图-如何用空格替换空值?
我正在为一些外部系统创建一个视图。此外部系统不使用空值,因此我希望将它们更改为更为用户友好的,例如“” 我使用此查询创建我的视图:Sql server 基于联接两个表的SQL Server视图-如何用空格替换空值?,sql-server,join,null,sql-view,isnull,Sql Server,Join,Null,Sql View,Isnull,我正在为一些外部系统创建一个视图。此外部系统不使用空值,因此我希望将它们更改为更为用户友好的,例如“” 我使用此查询创建我的视图: CREATE VIEW SomeView AS SELECT r.CountryRegionCode, r.Name, e.ExampleData FROM AdventureWorks2008.Person.CountryRegion r JOIN AdventureWorks2008.dbo.S
CREATE VIEW SomeView
AS
SELECT
r.CountryRegionCode, r.Name, e.ExampleData
FROM
AdventureWorks2008.Person.CountryRegion r
JOIN
AdventureWorks2008.dbo.SomeTable e ON r.CountryRegionCode = e.CountryRegionCode
此查询的结果是:
据我所知,我可以使用运算符将NULL替换为空格,但如何在语句中使用它呢?如何正确操作:
SELECT
ISNULL (r.CountryRegionCode, 0) AS r.CountryRegionCode
-- ..
?
更新:它不理解r
是什么:
更新#2:非常感谢大家!最终结果是:
SELECT
CountryRegionCode = ISNULL(r.CountryRegionCode, ''),
Name = ISNULL(r.Name,''),
ExampleData = ISNULL(e.ExampleData,'')
FROM
AdventureWorks2008.Person.CountryRegion r
JOIN
AdventureWorks2008.dbo.SomeTable e ON r.CountryRegionCode = e.CountryRegionCode
r
是一个表别名。如果您想获取列名r.CountryRegionCode
需要引用它们:[r.CountryRegionCode]
CREATE VIEW SomeView
AS
SELECT CountryRegionCode = ISNULL(r.CountryRegionCode, ' '), -- COALESCE(r.CountryRegionCode, ' ')
r.name,
e.ExampleData
FROM AdventureWorks2008.Person.CountryRegion r
JOIN AdventureWorks2008.dbo.SomeTable e ON r.CountryRegionCode = e.CountryRegionCode
另外,请注意COALESCE
和ISNULL
之间的区别:
DECLARE @a CHAR(1)
SELECT ISNULL(@a, 'NULL') /*N*/, COALESCE(@a, 'NULL') /*NULL*/
您可以这样做:选择ISNULL(r.CountryRegionCode“”)作为CountryRegionCode
DECLARE @a CHAR(1)
SELECT ISNULL(@a, 'NULL') /*N*/, COALESCE(@a, 'NULL') /*NULL*/