Need some SQL dates?

Need some SQL dates?

Just a handful of quickie SQL dates that might come in handy. Run this in Management Studio to return a bunch of commonly needed dates, such as first day of the month, last day of the month, etc.

Enjoy!

-- Date Format
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())),GETDATE()),101)

-- Datetime Format
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
-- Date Format
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101)

-- Datetime Format
SELECT DATEADD(mm, DATEDIFF(m,0,getdate()),0)
-- Date Format
SELECT CONVERT(VARCHAR(25),GETDATE(),101)

-- Datetime Format
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME)
-- Date Format
SELECT CONVERT(VARCHAR(25),
 DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),
 DATEADD(mm,1,GETDATE())),101)

-- Datetime Format
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
-- Date Format
SELECT CONVERT(VARCHAR(25),
 DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))-1),
 DATEADD(mm,1,GETDATE())),101)

-- Datetime Format
SELECT DATEADD(mm, DATEDIFF(m,0,getdate())+1,0)
Mike250

Australian. Sport. Passionate Cricket Fan. Go Pro. Abseiling. Snorkeling. Travel. Golf R. SQL Server Developer. Three sons. One daughter. Last Trip: New York.

Related Posts
Leave a comment

Your email address will not be published. Required fields are marked *