Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select query to retrieve data based on current date and time Expand / Collapse
Author
Message
Posted Thursday, March 10, 2011 5:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 5:42 AM
Points: 3, Visits: 1
I have problem in using SQL Server 2008. The new data types time and date are unknown to me. I cannot able to retrieve the datas from the database based on current time. I have two columns in the database table namely Date and Time whose data types are respectively date and time(0).
Now what i needed is to retrieve the data from the database based on date and time. As time changes it must retrieve the current date and time datas which are already available in the database.

Plz, help me in this regard. Any help from you will highly be appreciated.
Post #1076143
Posted Thursday, March 10, 2011 5:36 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:15 AM
Points: 177, Visits: 246
There are likely other ways to achieve this, but you can do the following:

Select * from TableName T
where T.Date = Convert(date,getdate())
and T.Time = CONVERT(time,getdate())

You get records where the date and time match the current date and time EXACTLY.


--------------------------------------------------------------------------
When you realize you've dug yourself into a hole....Step 1...stop digging.
Post #1076149
Posted Thursday, March 10, 2011 5:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 5:42 AM
Points: 3, Visits: 1
Thank u Sir for your immense help but when i execute the query i did not get any result. I want to ask one more question, whether the query must match the exact time i.e in hours, minute and seconds.
Actually can we do some kind of casting to match only the exact hours only, so that it will check for the hours from the Time column and get the data.

Thank you once again.
Post #1076160
Posted Thursday, March 10, 2011 5:59 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 2:55 PM
Points: 41,570, Visits: 34,495
Table definition, sample data and desired result please.

Using the time data type as Chaz showed requires matches to the nanosecond.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1076167
Posted Thursday, March 10, 2011 7:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 5:42 AM
Points: 3, Visits: 1
Actually, i have a table name district_data where the fields are district_id,Temperature, Rainfall,Wind, Date and Time.

district_id Temperature Rainfall Wind Date Time
1 17.5 5.0 8.0 2011-03-09 10:00:00
1 18.5 6.0 7.0 2011-03-09 11:00:00

It means the table contains the datas for every 1 hour interval. i must fetch the current time datas. I must have the value same for 10:00:00-10:59:59 i.e Temperature=17.5, when the time changes to 11:00:00 i must have Temperature=18.5, upto 11:59:59.
i must consider only the hour part. Plz, help me. I am trying it from 1 week, but still no sign of any success.
Plz, give me some suggestion so that i can continue.....

Thank you Sir, for giving me your time....
Post #1076636
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse