Solved

sql getDate not working

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Improvement  ( Speed) 14 27
Sql server get data from a usp to use in a usp 5 16
Better way to make a query with date filter. 5 27
SSRS Enable Remote Errors 4 26
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

773 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