Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax

Posted on 2014-03-06
5
Medium Priority
?
290 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
[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
  • 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

722 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