Share via

Help With Transact SQL ISNULL column fuction with addtional if null then if column 2 <= value then 'text'

Sullivan, Matthew 21 Reputation points
2020-10-20T20:12:09.25+00:00

Hello,

Looking for any help I can get, I've got a snippet of code where I need multiple "order of operations to happen" but continue to receive an error on my use of <=.

What I've written is:

ISNULL(state_customer, appended_state) as US_State

I have an additional column that shows distance in miles, what I need to do is combine my ISNULL function with "if US_State = null then if distance <= 100 then 'CA' else US_State.

Can anyone assist?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


3 answers

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2020-10-20T21:05:12.677+00:00

    This sounds to me like:

    CASE WHEN state_customer IS NOT NULL THEN state_customer
         WHEN distance <= 100 THEN 'CA'
         ELSE appended_state
    END
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 127K Reputation points
    2020-10-22T08:31:11.193+00:00

    Check this expression too: COALESCE(state_customer, appended_state, case when distance <= 100 then 'CA' end) as US_State.

    Was this answer helpful?

    0 comments No comments

  3. MelissaMa-msft 24,246 Reputation points Moderator
    2020-10-21T02:09:34.007+00:00

    Hi @Sullivan, Matthew ,

    For this type of problem we recommend that you could post CREATE TABLE statements for your tables together with INSERT statements with sample data, enough to illustrate all angles of the problem. We also need to see the expected result of the sample.

    According to your limited information, you could refer below example and check whether it is helpful.

    create table #t   
    (  
    state_customer varchar(20),  
    appended_state varchar(20),  
    distance int  
    )  
      
    insert into #t values  
    ('a','b',100),  
    (null,'c',100),  
    (null,null,100),  
    (null,null,200)  
      
    select case when ISNULL(state_customer, appended_state) IS NULL   
    		then case when distance <= 100 then 'CA'  end    
    		else ISNULL(state_customer, appended_state)  
    		end as US_State from #t  
    

    Output:

    US_State  
    a  
    c  
    CA  
    NULL  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.