Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax

Posted on 2014-03-06
5
Medium Priority
?
292 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 38

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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

886 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