Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

sql getDate not working

Posted on 2014-02-25
4
431 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL Challenge... 7 35
SQL query and VBA 5 45
Download ms sql express. 2 26
changing page verifacation 1 26
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

856 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