我有一个可为空的 varchar,即使它为空,我也希望我的选择返回一个空字符串而不是空字符串。那是
SELECT FIRST, LAST
FROM CUSTOMER
我想要的是如果FIRST
为空,我希望返回为""
我有一个可为空的 varchar,即使它为空,我也希望我的选择返回一个空字符串而不是空字符串。那是
SELECT FIRST, LAST
FROM CUSTOMER
我想要的是如果FIRST
为空,我希望返回为""
你也可以使用
SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER
这具有更便携的优点(COALESCE 是 SQL 标准的一部分,ISNULL 不是)
您还可以使用任意数量的参数,例如
SELECT COALESCE(FIRST, SECOND, THIRD, '')...
并且 COALESCE 将使用遇到的第一个非空值
SELECT isnull(FIRST,'') AS FIRST, LAST FROM CUSTOMER
select isNull(First,'') from customer
SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER
也可以
CREATE FUNCTION [dbo].[ParamParserFn]
(
@delimString VARCHAR(255) ,
@delim CHAR(1)
)
RETURNS @paramtable TABLE
(
[Index] INT ,
[Token] INT
)
AS
BEGIN
DECLARE @len INT ,
@index INT ,
@nextindex INT ,
@counter INT ,
@data VARCHAR(50)
SET @len = DATALENGTH(@delimString)
SET @index = 0
SET @nextindex = 0
SET @counter = 0
WHILE ( @len + 1 > @index )
BEGIN
SET @nextindex = CHARINDEX(@delim, @delimString, @index)
IF ( @nextindex = 0 )
SET @nextindex = @len + 1
INSERT @paramtable
SELECT @counter ,
SUBSTRING(@delimString, @index,
@nextindex - @index)
SELECT @index = @nextindex + 1 ,
@counter = @counter + 1
END
RETURN
END