[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

sql getDate not working

Posted on 2014-02-25
4
Medium Priority
?
450 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
[X]
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
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 93

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 60

Accepted Solution

by:
Kevin Cross earned 2000 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 25

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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