我有一个可以为空的varchar,即使它是null,我希望我的select返回一个空字符串而不是null。那是
SELECT FIRST, LAST
FROM CUSTOMER
我想要的是如果FIRST
为空,我希望返回为""
答案 0 :(得分:6)
您也可以使用
SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER
这具有更便携的优点(COALESCE是SQL标准的一部分,ISNULL不是)
您还可以使用任意数量的参数,例如
SELECT COALESCE(FIRST, SECOND, THIRD, '')...
COALESCE将使用遇到的第一个非空值
答案 1 :(得分:5)
SELECT isnull(FIRST,'') AS FIRST, LAST FROM CUSTOMER
答案 2 :(得分:1)
select isNull(First,'') from customer
答案 3 :(得分:1)
SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER
也会起作用
答案 4 :(得分:0)
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