Solved

SQL Date script help

Posted on 2013-11-20
6
321 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
  • 3
  • 2
6 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 69

Expert Comment

by:ScottPletcher
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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

912 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

15 Experts available now in Live!

Get 1:1 Help Now