Solved

ms sql + a conditional where statement

Posted on 2006-06-10
3
209 Views
Last Modified: 2008-02-01
Is it possible to have a conditional where statement if so how.

ie if a value in the select statement equals a something then the part of the where statement would equal one thing if not another

this is what i tried.
WHERE completed = 1
AND CASE WHEN touch_reason in (19,20) THEN
DATEDIFF(hour, cdol.maxdate , getDate()) > 12
ELSE
DATEDIFF(hour, cdol.maxdate , getDate()) > 0
END
0
Comment
Question by:dkilby
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 16878612
Hi dkilby,

WHERE completed = 1 AND
    ((touch_reason IN (19, 20) AND DATEDIFF(hour, cdol.maxdate, GETDATE()) > 12) OR
    (touch_reason NOT IN (19, 20) AND DATEDIFF(hour, cdol.maxdate, GETDATE()) > 0))

Regards,

Patrick
0
 

Author Comment

by:dkilby
ID: 16878637
thank you - worked great
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 16878951
dkilby,

You're welcome :)

Regards,

Patrick
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question