Solved

SQL 2005 need help with Time & Date

Posted on 2012-04-13
7
464 Views
Last Modified: 2012-06-22
Via MS SQL Server 2005

Hello everyone, I have a table that has a logdate column in this format 2012-04-13 07:42:59.387.
How would I select everything within only the current hour?

Reason is I am going to have a page that will refresh itself every minute and use this data to show the user what is going on in the current hour.
0
Comment
Question by:Alice7
  • 4
  • 2
7 Comments
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 37843070
where logdate > dateadd(h,-1,getdate()) and logdate <= getdate()
0
 

Author Comment

by:Alice7
ID: 37843234
Hello Dqmq, thank you for the help. I tried the suggested and got the following error.

Msg 155, Level 15, State 1, Line 2
'h' is not a recognized dateadd option.
0
 

Author Comment

by:Alice7
ID: 37843281
Dqmq I made the following change and it seems to be working now.

where logdate > dateadd(HH,-1,getdate()) and logdate <= getdate()
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 37843361
The query returns all entries within the last one hour.  If you need the records on the current hour only the query would be:


where logdate between dateadd(hour, datepart(hour, getdate()), cast(cast(getdate() as date) as datetime)) and getdate()
0
 

Author Comment

by:Alice7
ID: 37843732
Hello wsosanjos, I think all records on the current hour is what I am looking for. I tried your suggestion and got the following error.

Type date is not a defined system type.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37843892
That means you are on SQL 2005.  date was introduced with SQL 2008.

Please try: (actually a simpler solution)
where logdate between dateadd(hour, datediff(hour,0, getDate()), 0) and getdate()

Open in new window

0
 

Author Comment

by:Alice7
ID: 37844000
Thanks wdosanjos that worked perfect!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question