SQL Server Case statement conditions

  • Good afternoon,

    I am currently working on a data flow and have been given a specific requirement that i am trying to complete.

    In my table I have a column which is partially NULL due to a couple of reasons.

    What i'm trying to do is write a case statement within my select statement that has two conditions:

    When NULL use a different value from another column (which is pulled from another table using a join)

    If the column is still NULL (in both cases) then use a different value from another column in the table which will ensure the column is populated.

    So basically, if it's NULL do this, if its still NULL, then do this which will mean my column is populated as i intend.

    I've been playing around but have been unable to produce the required result. Is this something that can be achieved using a CASE statement?

    Any help/advice would be appreciated.

    Thanks.

  • You probably want to have a look at the COALESCE function:

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql?view=sql-server-ver15

    This is assuming that the query you have already has all of the required columns available in the joined worktable.  If  you have to run a query to lookup the value for any of the intermediate steps, this will fail the same as the CASE statement does.

  • COALESCE(partially_NULL_column, joined_table.column, another_column_in_table_thats_never_null)

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

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

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