| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-15 : 15:50:24
|
| I want to write a stored procedurewhere if @fromdate and @todate parameters are passed it should return the data based on date parametersIf nothing is passed it should return all the dataPlease help the below code to rewriteselect * from table where (dt Between @FROMDATE And @TODATE) AND (dt Between @FROMDATE And @TODATE) |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-15 : 15:52:41
|
| declare @FROMDATE datetime, @TODATE datetimeselect @FROMDATE = isnull(@FROMDATE, '17530101'), @TODATE = isnull(@TODATE, '99991231')select * from table where dt between @FROMDATE And @TODATE_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-15 : 17:27:31
|
| Sorry if i m not clear about my issue.I have write a store procedure with two conditions1.If @datefrom and @dateparameters values are passed sp should returned the values based on date values supplied.like(select * from table where dt between @FROMDATE And @TODATE)2.If no parameters are passed or null parameters are passed then it should retured the output like( select * from table)Create procedure usp_dtproc@fromdt datetime,@todt datetimeasif parameter value is fromdt and todt is trueselect * from table where dt between @FROMDATE And @TODATEelseIf parameter values are null then it should execute the below queryselect * form table |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-15 : 17:27:31
|
| Sorry if i m not clear about my issue.I have write a store procedure with two conditions1.If @datefrom and @dateparameters values are passed sp should returned the values based on date values supplied.like(select * from table where dt between @FROMDATE And @TODATE)2.If no parameters are passed or null parameters are passed then it should retured the output like( select * from table)Create procedure usp_dtproc@fromdt datetime,@todt datetimeasif parameter value is fromdt and todt is trueselect * from table where dt between @FROMDATE And @TODATEelseIf parameter values are null then it should execute the below queryselect * form table |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-15 : 17:51:23
|
| have you even tried my query?_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-15 : 19:32:43
|
| Thanks i got the desired results.... |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-15 : 20:00:28
|
no kidding? but do you know why?_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-16 : 00:28:17
|
| Other methodselect * from table where dt>=isnull(@FROMDATE,dt) And dt<=isnull(@TODATE,dt) MadhivananFailing to plan is Planning to fail |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2007-06-16 : 00:36:07
|
| Hi,Declare @F datetime , @T datetime--Select @f ='2016-03-12 00:00:00.000', @t = '2020-03-31 00:00:00.000'Select @f = NULL , @T = NULLSelect * From TableNamewhere ( ( @F is Null or Charge_From > = @f ) and (@T is Null or Charge_To <= @T)) |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-06-16 : 13:30:56
|
| When i tried with the below query i m not getting the desired output...when passing the nulls..declare @FROMDATE datetime, @TODATE datetimeselect @FROMDATE = isnull(@FROMDATE, '17530101'), @TODATE = isnull(@TODATE, '99991231')select * from table where CAST(CAST(YR AS VARCHAR) + RIGHT('0' + CAST(MM AS VARCHAR), 2) + RIGHT('0' + CAST(DD AS VARCHAR), 2) AS DATETIME) Between @FROMDATE And @TODATE |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2007-06-17 : 14:28:19
|
| Hi,How about this one?Select * from table where((@fromdate is null) and (@todate is null)or (dt between @fromdate and @todate))But while executing the stored proc if the parameters are nullyou need to write like thisexec dtproc @fromdate=null,@todate=nullThanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-18 : 01:14:03
|
quote: Originally posted by sqldba2k6 When i tried with the below query i m not getting the desired output...when passing the nulls..declare @FROMDATE datetime, @TODATE datetimeselect @FROMDATE = isnull(@FROMDATE, '17530101'), @TODATE = isnull(@TODATE, '99991231')select * from table where CAST(CAST(YR AS VARCHAR) + RIGHT('0' + CAST(MM AS VARCHAR), 2) + RIGHT('0' + CAST(DD AS VARCHAR), 2) AS DATETIME) Between @FROMDATE And @TODATE
Dont use convert and tryMadhivananFailing to plan is Planning to fail |
 |
|
|
|