Using Case Statement in Where clause for datediff function

Last post 07-18-2008 10:06 AM by cgreene.gsu. 9 replies.

Sort Posts:

  • Using Case Statement in Where clause for datediff function

    07-18-2008, 8:38 AM
    • Loading...
    • cgreene.gsu
    • Joined on 07-18-2008, 8:09 AM
    • Posts 9
    I am trying to use a case statement in my where clause to do a datediff so that when my close_date is null it will use getdate() instead of the close_date column. With the code poste below I receive the following error:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '>'.

    SELECT [supp_call_tckt_num], UPPER([assigned_to]) as assigned_to, assigned_date, close_date, diffday = CASE WHEN close_date is null then datediff(d,assigned_date,getdate()) else datediff(d,assigned_date,close_date) end, UPPER([call_source]) as call_source, UPPER([account_type]) as account_type, [mdn], UPPER([status]) as status, UPPER([company_name]) as company_name, UPPER([rep_id]) as rep_id, UPPER([caller_first_name]) caller_first_name, UPPER([caller_last_name]) as caller_last_name, [account_number], UPPER([network]) as network, UPPER([issue_type]) as issue_type, UPPER([problem]) as problem, UPPER([subtype]) as subtype, UPPER([prob_desc_res]) as prob_desc_res, UPPER([notes]) as notes, [ticket_number] FROM [ewts_tracker] with (NOLOCK) WHERE [ewts_tracker].[escalated] = 'Yes' AND close_date = CASE WHEN close_date IS NOT NULL THEN datediff (d,assigned_date,close_date) >= '3' else datediff(d,assigned_date,getdate()) >= '3' END

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 8:55 AM

    Try this: 

    SELECT [supp_call_tckt_num], UPPER([assigned_to]) as assigned_to, assigned_date, close_date, 
    	DATEDIFF(d, assigned_date, ISNULL(close_date, getdate())) as diffday, 
    	UPPER([call_source]) as call_source, UPPER([account_type]) as account_type, [mdn], UPPER([status]) as status, UPPER([company_name]) as company_name, UPPER([rep_id]) as rep_id, 
    	UPPER([caller_first_name]) caller_first_name, UPPER([caller_last_name]) as caller_last_name, [account_number], UPPER([network]) as network, 
    	UPPER([issue_type]) as issue_type, UPPER([problem]) as problem, UPPER([subtype]) as subtype, UPPER([prob_desc_res]) as prob_desc_res, 
    	UPPER([notes]) as notes, [ticket_number] 
    FROM [ewts_tracker] with (NOLOCK) 
    WHERE [ewts_tracker].[escalated] = 'Yes'
    AND close_date = datediff(d, assigned_date, ISNULL(close_date, getdate())) >= 3 
     
  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:02 AM

    cgreene.gsu:
    (d,assigned_date,close_date) >= '3' else datediff(d,assigned_date,getdate()) >= '3' END
     

    here the error is in last line. change it to

    (d,assigned_date,close_date) else datediff(d,assigned_date,getdate()) END

    I didn't understand, what hou wants to do here 

    Syntax is like

    case

            when condition1 then result1

            else result2      

    end

    you given like

    case

       when condition1 then condition2

        else condition3

    end

     

     

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:13 AM
    • Loading...
    • cgreene.gsu
    • Joined on 07-18-2008, 8:09 AM
    • Posts 9

    That didnt work still gives the incorrect syntax near '>' error.  And what I am trying to do is if my close_date column has a field that is null I want it to compare my assigned_date column to getdate() instead of comparing assigned_date to close_date

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:25 AM

    Check that you are typing correctly - the query I gave you should work fine!

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:36 AM

    SELECT [supp_call_tckt_num], UPPER([assigned_to]) as assigned_to,

    assigned_date, close_date, diffday = CASE WHEN close_date is null then

    datediff(d,assigned_date,getdate()) else

    datediff(d,assigned_date,close_date) end,

    UPPER([call_source]) as call_source,

    UPPER([account_type]) as account_type, [mdn],

    UPPER([status]) as status, UPPER([company_name]) as company_name,

    UPPER([rep_id]) as rep_id, UPPER([caller_first_name]) caller_first_name,

    UPPER([caller_last_name]) as caller_last_name, [account_number],

    UPPER([network]) as network, UPPER([issue_type]) as issue_type,

    UPPER([problem]) as problem, UPPER([subtype]) as subtype, UPPER([prob_desc_res])

    as prob_desc_res, UPPER([notes]) as notes, [ticket_number] FROM

    [ewts_tracker] with (NOLOCK) WHERE [ewts_tracker].[escalated] =

    'Yes' AND

    datediff (d,isnull(assigned_date,getdate()),close_date) >= '3'

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:38 AM
    • Loading...
    • cgreene.gsu
    • Joined on 07-18-2008, 8:09 AM
    • Posts 9

    i copied and pasted your code and it still errors out I remove everything after the AND, then it pulled up fine

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:45 AM
    • Loading...
    • cgreene.gsu
    • Joined on 07-18-2008, 8:09 AM
    • Posts 9

    datediff (d,isnull(assigned_date,getdate()),close_date) >= '3'  Worked perfectly thank you

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 9:46 AM
    Answer

    Oh, my stupidity. The last line reads:

    AND close_date = datediff(...) >= 3

    Which is just a nonsense.

    Remove the close_date and make it:

    AND datediff(...) >= 3

  • Re: Using Case Statement in Where clause for datediff function

    07-18-2008, 10:06 AM
    • Loading...
    • cgreene.gsu
    • Joined on 07-18-2008, 8:09 AM
    • Posts 9

    MelvynHarbour this worked perfect thank you. the other one that worked only pulled back the fields that have a close_date

Page 1 of 1 (10 items)
Microsoft Communities
Page view counter