?
Solved

SQL Date script help

Posted on 2013-11-20
6
Medium Priority
?
342 Views
Last Modified: 2013-11-20
I'm needing to get the 15th of last month.  The script I currently have gets me the 15th for current month.

select cast(convert(varchar(10), dateadd(dd,datediff(dd,datepart(dd,getdate()),15),getdate()), 101) as datetime)
0
Comment
Question by:abarefoot
[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
  • 3
  • 2
6 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39663429
SELECT DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0))

--Remove the "- 1" to get the 15th of the current month
0
 
LVL 1

Author Closing Comment

by:abarefoot
ID: 39663452
Thanks for the help.  That's what I needed.
0
 
LVL 1

Author Comment

by:abarefoot
ID: 39663576
one other question.  How would I get this to show time as 23:59 and not 00:00.  In other words at the end of day not beginning.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39663834
For the 15th, and as 23:59[:00.000], then like this:

SELECT DATEADD(MINUTE, -1, DATEADD(DAY, 15, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)))

If you want 23:59:59.997 (for a datetime rather than a smalldatetime), change "MINUTE, -1" to "MILLISECOND, -3".
0
 
LVL 1

Author Comment

by:abarefoot
ID: 39663863
Thanks
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39665076
>>"How would I get this to show time as 23:59"
mmmm, this indicates you are using "between" and that can be a problem

if you are using the 15th of a Month calculation for selecting dates ranges

e.g.

xyz between '2013-10-15' and '2013-11-14 23:59:59.997'

Then the WAY easier and more accurate method is to stop using between, e.g.

( xyz >= '2013-10-15' and xyz < '2013-11-15' )

for more on this: "Beware of Between"
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

719 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