Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 date in where clause

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-11-02 : 08:04:33
Hi all :

I need to do following using T-SQL

If created_at_begin <> Null and created_at_end is Null then the where clause adds Created_At>= created_at_begin. If created_at_begin = Null and created_at_end <> Null then the where clause adds Created_At<= created_at_end. If created_at_begin <> Null and created_at_end<> Null then the where clause adds Created_At between created_at_begin and created_at_end.

where created_at_begin and created_at_end are the parameters.

Regards,
Asif Hameed

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-02 : 08:29:58
where created_at <= isNull(@created_at_end, created_at)
and created_at >= isNull(@created_at_begin, created_at)

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-02 : 08:51:38
or

where (@created_at_end is null or created_at <= @created_at_end)
and (@created_at_begin is null or created_at >= @created_at_begin)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-11-02 : 08:53:34
what about the between clause ?
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-02 : 11:28:06
When both values are supplied then the two conditions together act as BETWEEN. Try it...

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -