SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


making where clause dynamic


making where clause dynamic

Author
Message
SQL_Surfer
SQL_Surfer
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 1086
Can we make where clause dynamic?

declare @hourly int

if @hourly = 1
--I want the where clause to say
TableA.LoadDateTime >= @TodaysDate
if @hourly = 0
--I want the where clause to say
convert(varchar(8),TableA.LoadDateTime,112) >= convert(varchar(8), @TodaysDate,112)


select col1, col2 from TableA
where TableA.LoadDateTime >= @TodaysDate
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32284 Visits: 18552
To get a dynamic where clause you would need to use dynamic sql and execute that sql string.

Or you could have both queries and an if else statement. Based on the conditions you provide, either one or the other query will be executed.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Sowbhari
Sowbhari
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 1486
Will this help...

DECLARE @Hourly INT
SELECT @Hourly = 1
SELECT Col1,Col2 FROM TableA
WHERE
(CASE WHEN @Hourly = 1 THEN TableA.LoadDateTime
ELSE CONVERT(VARCHAR(8),TableA.LoadDateTime,112)
END) >=
(CASE WHEN @Hourly = 1 THEN @TodaysDate
ELSE CONVERT(VARCHAR(8),@TodaysDate,112)
END)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search