B.H
B.H

Reputation: 33

Converting Year Day Month to Month Day

Wondering if anyone could help with a problem I am getting in SQL Server.

Below is what I am trying to change date from 2016-09-01 to September 01 etc.

I am getting an error message when trying to convert the date.

set @start_date = '2016-09-01'
set @end_date = '2016-11-21'

SELECT pol AS [Policy], 
DATENAME(MONTH, pol_dt) + ' ' + CAST(DATEPART(DAY, pol_dt)AS VARCHAR(2)) AS [Policy Date]
from pol_t
where pol_dt>= @start_date
and pol_dt<= @end_date

Upvotes: 3

Views: 82

Answers (3)

user7351900
user7351900

Reputation:

Month DD 1

SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2)) AS [Month DD]

January 2

This is a great link for all things related to SQL Server and Date formatting.

http://www.sql-server-helper.com/tips/date-formats.aspx

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 81960

IF 2012+ You could use format

Declare @D Date='2016-09-01'
Select Format(@D,'MMMM dd')

Returns

September 01

Format() has some great functionality, but it is not known for its performance

Upvotes: 1

Ankitkumar Tandel
Ankitkumar Tandel

Reputation: 309

try this ...

SELECT DATENAME(MONTH, SYSDATETIME())+ ' ' + RIGHT('0' + DATENAME(DAY, SYSDATETIME()), 2) AS [Month DD];

Upvotes: 0

Related Questions