Solved

sql getDate not working

Posted on 2014-02-25
4
421 Views
Last Modified: 2014-03-05
Hi

I have the following SQL
 Select ID from UsageLogs where BranchId = 51 AND MachineId = 133 AND ReportedDate = GETUTCDATE()

Open in new window

I have also tried GetDate()
but i get 0 results
even though i know there is a record in table with the right info

The date is stored as

2014-02-25 17:21:03.8530000
I'm not bothered about time, i just want to match the date

any ideas?
0
Comment
Question by:websss
4 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 39885649
Select ID from UsageLogs where BranchId = 51 AND MachineId = 133 AND CONVERT(varchar, ReportedDate, 101) = CONVERT(VARCHAR, GetDate(),101)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39885790
This will be a little more index-friendly:

DECLARE @StartDate datetime, @EndDate datetime

SET @StartDate = DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))
SET @EndDate = DATEADD(day, 1, @StartDate)

Select ID 
from UsageLogs 
where BranchId = 51 AND MachineId = 133 AND 
    ReportedDate >= @StartDate AND ReportedDate < @EndDate

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39885802
I would avoid converting your column to character strings.  I would leave your dates as dates.  Instead use ranges.
SELECT ID 
FROM UsageLogs 
WHERE BranchId = 51 AND MachineId = 133 
AND ReportedDate >= DATEADD(DD, DATEDIFF(DD, 0, GETUTCDATE()), 0)
AND ReportedDate < DATEADD(DD, DATEDIFF(DD, 0, GETUTCDATE())+1, 0)
;

Open in new window

This pulls every time stamp between midnight of the day you request and the next day.

If you have SQL 2008 or higher, you can use the DATE data type which has no time.

CONVERT(DATE, GETUTCDATE()) and CONVERT(DATE, DATEADD(DD, 1, GETUTCDATE())) can replace the DATEDIFF calculations.

EDIT: I just saw Patrick posted the same suggestion, so sorry for the duplication.
0
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 39887444
Yes, do not use Convert in the where clause on the column in the table, you are guaranteed to have a table scan.  It is better to do a range as was illustrated.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now