Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

using case when condition in WHERE in SP Expand / Collapse
Author
Message
Posted Friday, September 27, 2013 11:41 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 7:28 AM
Points: 75, Visits: 197
i have a SP with where condition
as where id=@id and month=@month and Eid=@Eid
wht i need is if @Eid=0 i need to display all the reocrds
if Eid>0 then display only that particular reocrd
i tried

where id=@id and month=@month and Eid=
case when @Eid=0 then Eid>0
else
Eid=@Eid
End
but its giving me error
Incorrect syntax near '>'.
Post #1499638
Posted Saturday, September 28, 2013 2:15 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:41 PM
Points: 22,491, Visits: 30,185
ssurekha2000 (9/27/2013)
i have a SP with where condition
as where id=@id and month=@month and Eid=@Eid
wht i need is if @Eid=0 i need to display all the reocrds
if Eid>0 then display only that particular reocrd
i tried

where id=@id and month=@month and Eid=
case when @Eid=0 then Eid>0
else
Eid=@Eid
End
but its giving me error
Incorrect syntax near '>'.


You mean something like this?


...
where
id = @id and
month = @month and
((@Eid = 0) or (Eid = @Eid))





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1499654
Posted Monday, September 30, 2013 2:02 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, December 18, 2013 5:03 AM
Points: 1,454, Visits: 135
Hi,

Try with this code

where id=@id and month=@month and Eid=
case when @Eid=0 then case when Eid>0 then Eid else 0 end else @Eid end =Eid

Post #1499886
Posted Monday, September 30, 2013 6:23 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 7:28 AM
Points: 75, Visits: 197
getting incorect syntax near =
Post #1499954
Posted Monday, September 30, 2013 6:25 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:41 PM
Points: 22,491, Visits: 30,185
ssurekha2000 (9/30/2013)
getting incorect syntax near =


Have you tried what I posted earlier?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1499955
Posted Monday, September 30, 2013 6:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
parulprabu (9/30/2013)
Hi,

Try with this code

where id=@id and month=@month and Eid=
case when @Eid=0 then case when Eid>0 then Eid else 0 end else @Eid end =Eid



There's a syntax error and a pointless logical check in this code. Can you identify them, parulprabu?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1499959
Posted Monday, September 30, 2013 10:31 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, December 18, 2013 5:03 AM
Points: 1,454, Visits: 135
parulprabu (9/30/2013)
Hi,

Try with this code

where id=@id and month=@month and Eid=
case when @Eid=0 then case when Eid>0 then Eid else 0 end else @Eid end =Eid



Sorry for the erroneous solution given. This is the correct one

where id=@id and month=@month and Eid=
(case when @Eid=0 then case when Eid>0 then Eid else 0 end else @Eid end)

Post #1500259
Posted Monday, September 30, 2013 11:38 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:41 PM
Points: 22,491, Visits: 30,185
I really hate to rain on everyone's parade but it isn't necessary to use a CASE to accomplish what is asked.

Please look at the solution I posted in my original response.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1500263
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse