.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Records Between a Perticular Range

Posted By: Santosh     Posted Date: October 10, 2009    Points:2   Category :Sql Server
hi
can any plz help me how to find records between a given Date Time Range.

Ex:Let there are two columns in Table Like(FromDate(DateTime),ToDate(DateTime))
Table Records Are Like
ID FromDate ToDate
1 12/09/2008 12:10:00 AM 07/04/2009 12:10:00 AM
2 24/12/2008 12:10:00 AM 16/01/2009 10:40:12 PM

etc

now if i am passing fromDate = 12/08/2006 and ToDate = 12/12/2009 then it should come come all the records between this range(checking Time Also)
Plz Help Me,Urgent
Santosh,Bangalore

Responses
Author: Amit Mehra             
Posted Date: October 10, 2009     Points: 5   

Hi Santosh,

You can build your SQL Query like this

Select col1,col2,col3 from table_Name where dateCol1 >= @Param1 and dateCol1 < @Param2


in the aboev query @Param1 and @Param2 is parameter From and To datetime input

Thanks
Amit

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Santosh             
Posted Date: October 10, 2009     Points: 5   

Hi Amit
i m passing @Param1 and @Param2 in ("12/27/2008") Fromat means varchar then how it will check.

and if @Param1 and @Param2 values i declare as datetime also it is not working.

plz give me with a tested example

Thanx
Santosh

Author: Amit Mehra             
Posted Date: October 10, 2009     Points: 5   

Hi Santosh,

the above sample query is used in stored procedure..if you want direct SQL statement than try like this

Select col1,col2,col3 from table_Name where dateCol1 >= '9/12/2008 ' and dateCol1 < '12/24/2008'


Here you have to care about passing the datetime because by default SQL server take like MM/dd/yyyy and you have to pass like this only..

Hope i understood your question correctly

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Santosh             
Posted Date: October 11, 2009     Points: 5   

No Amit,what u given that is not working.
i think u r not getting my question

i need the record in between the range and it should check not only Date but Time also.
so we need to use DateDiff function but o am not getting the correct result

Thanx
Santoh

Author: Amit Mehra             
Posted Date: October 11, 2009     Points: 5   

Hi Santhosh,

I have tested the above query and its working fine...as i recommend you have pass the input parameter in MM/dd/yyyy hh:mm.ss and yes you don't have to use datediff() function...Please take a look at it...just now i have tested again and its working fine....

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Santosh             
Posted Date: October 12, 2009     Points: 5   

Thanx Amit,it's working Fine
Once Again Thank u.

Regards
Santosh

Author: Narayanan             
Posted Date: July 20, 2011     Points: 20   

use BETWEEN in your query
SELECT * FROM tbl_Login WHERE Fdate BETWEEN '2005-01-01' AND '2005-12-31'
Refer :http://www.plus2net.com/sql_tutorial/between-date.php

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend