4

I have a nullable varchar and even when it is null, I want my select to return an empty string and not null. That is

SELECT FIRST, LAST 
FROM CUSTOMER

What I want is if FIRST is null, I want the return to be ""

5 Answers 5

6

You can also use

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

This has the advantage of being more portable (COALESCE is part of the SQL Standard, ISNULL isn't)

You can also use an arbitrary number of arguments, like

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

And COALESCE will use the first non-null value encountered

Sign up to request clarification or add additional context in comments.

Comments

6
SELECT isnull(FIRST,'') AS FIRST, LAST FROM CUSTOMER

Comments

1
select isNull(First,'') from customer

Comments

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

would work also

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.