Checking for Null values

  • I need to create a query to find all customer records by the email address. There might be an input parameter or not. It not I need to find all but still keep it in the query because its going to be a stored procedure. I can only get the case one to work but shouldn't the others work to get all records too?

    SELECT cusid

    , [name]

    , CASE WHEN emailwork IS NULL THEN '' ELSE emailwork END email

    FROM cus

    WHERE 'email' LIKE '%'

    SELECT cusid

    , [name]

    , ISNULL(emailwork, '') email

    FROM cus

    WHERE emailwork LIKE '%'

    SELECT cusid

    , [name]

    , COALESCE(emailwork, '') email

    FROM cus

    WHERE emailwork LIKE '%'

  • Yes, they all should work. What errors are you getting.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • in last 2 queries if you remove where clause you will get same result in all 3 queries. I tried and it worked fine.

  • Well, that is because the Where clause of your first query is not the same as the other two. Your first query:

    ...

    WHERE 'email' LIKE '%'

    versus the other two:

    ...

    WHERE emailwork LIKE '%'

    ...

    WHERE emailwork LIKE '%'

    You will notice that the first WHERE clause is non-functional: it will always be true, so, yes, removing the other Where clauses would make them work just the same.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply