Solved

SQL Syntax

Posted on 2014-03-06
5
284 Views
Last Modified: 2014-03-06
I'm trying to understand someone else's syntax: -

These are the parameters: -
      DECLARE @TodayStart AS DATETIME, @TodayEnd AS DATETIME
      SET @TodayStart = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
      SET @TodayEnd = DATEADD(dd, 1, @TodayStart)

This is the 'where' part of the query: -
      WHERE [ReceiptDate] >= '''+@TodayStart+'''
      AND [ReceiptDate] < '''+@TodayEnd+'''

Can anyone tell me what date range will be returned by this query?
0
Comment
Question by:HKFuey
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39908910
lol ... i guess this are the same 2 lines
SET @TodayStart = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

SET @TodayStart = getdate()


daterange is from today until tomorrow
0
 

Author Comment

by:HKFuey
ID: 39908920
So, even though it is convoluted, the results will be transactions from 'Today'?
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
ID: 39908933
Hello,

SET @TodayStart = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

This statement sets @TodayStart as beginning time of today, like '2014/03/06 00:00:00'
Below statement adds one day to @TodayStart which will set @TodayEnd to beginning time of tomorrow, like '0314/03/07 00:00:00'
SET @TodayEnd = DATEADD(dd, 1, @TodayStart)

And below final clause will return all records with ReceiptDate falling any time today. This is required only when you have time part in ReceiptDate column:
WHERE [ReceiptDate] >= '''+@TodayStart+'''
      AND [ReceiptDate] < '''+@TodayEnd+'''


-Harish
0
 

Author Closing Comment

by:HKFuey
ID: 39908938
That's very clear, thanks very much!
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39908939
You can also replace   SET @TodayStart = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
with   SET @TodayStart = convert(varchar, getdate(), 111), which is just trimming the time part from datetime value.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

776 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