在Sql Server中的条件之间 [英] Between Condition In Sql Server
问题描述
大家好,
我的查询是
Hi All,
My Query IS
SELECT * FROM TableName WHERE Date Between Date1 AND Date2 AND ID=ISNULL(@ID,ID)
这里我通过在where where条件下使用isnull来避免动态查询..现在我可以使用或Date搜索..当日期未通过时 Date1和Date2之间的日期这个必须通过...就是朋友的情况..
当date为null时......条件应该只基于ID。在案件之间应该传递真实。希望你明白!!!!
谢谢,
SarathKumar.N
Here I have avoided dynamic query by using isnull in where condition.. Now i can search using or Date .. When Date is Not Passed the Date Between Date1 AND Date2 this have to pass... that is the case friends..
when date is null .. the condition should have to based on ID only. and between case should pass as true.. Hope U understand!!!!
Thanks,
SarathKumar.N
推荐答案
嗨朋友们,
感谢您的帮助....我已经解决了这个问题
Hi Friends,
Thank u for ur help.... And I have Solved this
SELECT * FROM tablename WHERE Date BETWEEN ISNULL(@Fromdate,Date) AND ISNULL(@ToDate,ISNULL(@Fromdate,Date))
AND ID =ISNULL(@Id,ID)
此查询为我工作..感谢朋友们的贡献.. ..
This Query Worked Out For Me.. Thanks Friends for ur contribution....
您可以将null值传递给过程。请检查以下主题:
http://stackoverflow.com/questions/1207404/how-to-pass-a-null-variable-to-a-sql-存储过程来自网络代码 [ ^ ]
You can pass null value to the procedure. please check the following thread:
http://stackoverflow.com/questions/1207404/how-to-pass-a-null-variable-to-a-sql-stored-procedure-from-c-net-code[^]
这个技巧将工作
this trick will work
select * from a
where
(startdate between @t1 and @t2 or (@t1 is null or @t2 is null))
and
(id=@Id or id is null)
快乐编码!
:)
Happy Coding!
:)
这篇关于在Sql Server中的条件之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!