Solved

SQL Syntax

Posted on 2014-03-06
5
280 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 36

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 shrink a transaction log file down to a reasonable size.

758 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

19 Experts available now in Live!

Get 1:1 Help Now