3

我有一个可为空的 varchar,即使它为空,我也希望我的选择返回一个空字符串而不是空字符串。那是

SELECT FIRST, LAST 
FROM CUSTOMER

我想要的是如果FIRST为空,我希望返回为""

4

5 回答 5

6

你也可以使用

SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER

这具有更便携的优点(COALESCE 是 SQL 标准的一部分,ISNULL 不是)

您还可以使用任意数量的参数,例如

SELECT COALESCE(FIRST, SECOND, THIRD, '')...

并且 COALESCE 将使用遇到的第一个非空值

于 2012-09-18T00:50:39.863 回答
5
SELECT isnull(FIRST,'') AS FIRST, LAST FROM CUSTOMER
于 2012-09-18T00:48:52.133 回答
1
select isNull(First,'') from customer
于 2012-09-18T00:49:41.673 回答
1
SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER

也可以

于 2012-09-18T00:51:14.560 回答
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
于 2013-07-16T18:24:37.237 回答