SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL code


SQL code

Author
Message
eobiki10
eobiki10
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 369
Hi All,

Can someone help me interpret what this code is doing.

open_date BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) - '00:00:01'

and is there another way to re-write it.

EO
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50211 Visits: 10844
eobiki10 (12/5/2013)
Hi All,

Can someone help me interpret what this code is doing.

open_date BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) - '00:00:01'

and is there another way to re-write it.

EO

It looks to me like it's part of a WHERE clause that filters for rows where open_date is in the current month including time of day.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42594 Visits: 19845
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0)


Is getting the first day of previous month.

DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)


Is getting the first day of current month.

 - '00:00:01'


Is substracting one second to get only values from previous month because BETWEEN limits are inclusive.

There are ways to rewrite the code, but this might be the best in terms of performance and flexibility. It's basically adding months to a base date (zero or 1900-01-01) to eliminate days and time. You could use the same functionality for other datetime parts.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50211 Visits: 10844
Luis Cazares (12/5/2013)
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0)


Is getting the first day of previous month.

DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)


Is getting the first day of current month.

 - '00:00:01'


Is substracting one second to get only values from previous month because BETWEEN limits are inclusive.

There are ways to rewrite the code, but this might be the best in terms of performance and flexibility. It's basically adding months to a base date (zero or 1900-01-01) to eliminate days and time. You could use the same functionality for other datetime parts.

DOH! I typed current month. Blush Luis is right - it's previous month. Thanks for catching it, Luis.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search