Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select query to retrieve data based on current date and time


Select query to retrieve data based on current date and time

Author
Message
pratim.phukan
pratim.phukan
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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.
ChazMan
ChazMan
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 247
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.
pratim.phukan
pratim.phukan
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)SSC Guru (58K reputation)

Group: General Forum Members
Points: 58985 Visits: 44725
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, MVP, M.Sc (Comp Sci)
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


pratim.phukan
pratim.phukan
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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....
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search