convert datetime to display full month - sql

I have searched and i cannot seem to find the answer. I have tried using different styles, but none of them give me exactly what i want.
I am trying to convert a datetime, that I have calculated. However I need it to display the full month.
SELECT CONVERT(VARCHAR(25),DATEADD(dd, #codelife, getdate()),107)
the above line works fine, EXCEPT it displays the date like
Feb 27, 2014
I need it to display date like
February 27, 2014 etc.....
any suggestions?

Nathan's answer seems solid, however if you're using SQL Server 2012 you can make use of the FORMAT() function:
SELECT FORMAT(GETDATE(),'MMMM dd, yyyy')
Or in your case:
SELECT FORMAT(DATEADD(dd, #codelife, getdate()),'MMMM dd, yyyy')

I always use http://www.sql-server-helper.com/tips/date-formats.aspx as my reference
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
I would declare your date as a variable and do something like:
DECLARE #myDate DATETIME
SET #myDate = DateAdd(dd, #codelife, getdate());
SELECT DATENAME(MM, #myDate) + RIGHT(CONVERT(VARCHAR(12), #myDate, 107), 9) AS [Month DD, YYYY]

You may have to do it in 2 steps.
DECLARE #codelife INT = 120
DECLARE #codedate DATETIME = (SELECT (DATEADD(D, #codelife, GETDATE())))
SELECT DATENAME(MM, #codedate) + SUBSTRING(CONVERT(VARCHAR(25), #codedate, 107), 4, 25)

SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
I answered the same question a couple of days ago have a look here sql server Get the FULL month name from a date

Related

Produce output of previous year and month correctly

This sql was created to check the date but when I run a test for 2 month ago the output is 201700 and not 201612.
DECLARE #PeriodID AS CHAR(6)
SELECT #PeriodID = CONVERT(char(4), YEAR(GETDATE())) +
CASE
WHEN MONTH(GETDATE())<10 THEN '0' +CONVERT(char(2),MONTH(GETDATE())-2)
ELSE CONVERT(char(2),MONTH(GETDATE())-1)
END
print #periodid
I agree with Tim's answer, however the code he wrote is a bit combersome.
The following code should provide the same result, using only one DateAdd and one Convert:
DECLARE #PeriodID AS CHAR(6)
SELECT #PeriodID = CONVERT(CHAR(6), DATEADD(MONTH, -2, GETDATE()), 112)
PRINT #PeriodID
Results (for today - 2017-02-02):
201612
I think the simplest way to handle this is just to compute the date for 2 months ago, and then format the output with only the year and month, as your question implies:
SELECT CONVERT(CHAR(4), YEAR(DATEADD(month, -2, GETDATE()))) +
RIGHT('00' + CAST(MONTH(DATEADD(month, -2, GETDATE())) AS CHAR(2)), 2) AS periodId
FROM yourTable

SQL Server - converting date to D-MMM-YYYY format

How can I get the string from GETDATE() in D-MMM-YYYY format, e.g 3 May 2016
If I use CONVERT(VARCHAR, GETDATE(), 106), I would get a leading zero on day which is not what I want.
If you are on SQL Server 2012 or later, use FORMAT:
SELECT FORMAT(GETDATE(), 'd MMM yyyy')
Edit: some of the answers below are just flat-out wrong so I'm adding a solution for older versions of SQL Server. 2005 is the earliest that I can get my hands on:
SELECT CASE
WHEN CONVERT(varchar(20), GETDATE(), 106) LIKE '0%'
THEN SUBSTRING(CONVERT(varchar(20), GETDATE(), 106), 2, 20)
ELSE CONVERT(varchar(20), GETDATE(), 106)
END
SELECT case when left(convert(varchar(20),[DateColumn],106),1) ='0'
then right(convert(varchar(20),[DateColumn],106),len(convert(varchar(20),[DateColumn],106))-1)
else convert(varchar(20),[DateColumn],106)
end
FROM [DB].[dbo].[Table]
Some sample output :
29 Apr 2016
2 Apr 2016
If you cannot use FORMAT (Below SQL Server 2012)
DECLARE #date DATE = '20160503'
SELECT REPLACE(DATEPART(DAY, #date),' 0','') + ' ' +
CONVERT(CHAR(3), #date, 0) + ' ' +
CAST(DATEPART(YEAR, #date) AS CHAR(4))

Datetime Format in Month and Year

I want to format a datetime column like so: "March 2004." Currently, I tried
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT CONVERT(VARCHAR(20),CAST(#Date AS DATETIME),13) AS DateFormats
but not getting the right result.
You should really apply formatting when you present your data, not in the query, but to answer the question, if you're using SQL Server 2012 or above you can use FORMAT
DECLARE #Date datetime = '2004-03-05 01:00'
SELECT FORMAT( #Date, 'MMMM yyyy' ) AS DateFormats
Something like:
DECLARE #Date VARCHAR(20) = '2004-03-05 01:00'
SELECT DATENAME(MONTH, #Date) + ' ' + DATENAME(YEAR, #Date)
More on DATENAME can be found here https://msdn.microsoft.com/en-gb/library/ms174395.aspx
Essentially it gets the month part of the date and the year part of the date and concatenate them together.
None of the built-in formats include the full month. Instead, use datename():
select datename(month, #date) + ' ' + datename(year, #date)

How can I use GETDATE() to get month and year from the past month?

How can I get it to one statement that can get me
last month and this year?
I have a INSERT INTO and in a column report_date [datetime]
insert into table_a (report_date) values ( ??);
i want to show this past month and year,
So for example today is 4/21/2014
so it would show 3/2014 in the column
If today was MAY 1 2014 , it would show 4/2014?
Is this possible or does it have to have a day?
You're looking for the DATEADD function:
SELECT DATEPART(month, DATEADD(month, -1, GETDATE()), DATEPART(year, DATEADD(month, -1, GETDATE())
If you are trying to get 'last month' with 'this year' you could do:
SELECT CAST(
CAST(YEAR(GETDATE()) AS VARCHAR) + '-' +
CAST(MONTH(GETDATE())-1 AS VARCHAR) + '-' +
CAST(DAY(GETDATE()) AS VARCHAR)
AS DATETIME)
But then in JAN 2015, it would return DEC 2015.
If you just want last month, use the 'DATEADD' function.
SELECT DATEADD(MONTH, -1, GETDATE())

sql server Get the FULL month name from a date

How do I use sql to get the whole month name in sql server?
I did't find a way using DATEPART(mm, mydate) or CONVERT(VARCHAR(12), CreatedFor, 107).
Basically I need in the format: April 1 2009.
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
If you are using SQL Server 2012 or later, you can use:
SELECT FORMAT(MyDate, 'MMMM dd yyyy')
You can view the documentation for more information on the format.
Most answers are a bit more complicated than necessary, or don't provide the exact format requested.
select Format(getdate(), 'MMMM dd yyyy') --returns 'October 01 2020', note the leading zero
select Format(getdate(), 'MMMM d yyyy') --returns the desired format with out the leading zero: 'October 1 2020'
If you want a comma, as you normally would, use:
select Format(getdate(), 'MMMM d, yyyy') --returns 'October 1, 2020'
Note: even though there is only one 'd' for the day, it will become a 2 digit day when needed.
109 - mon dd yyyy (In SQL conversion)
The required format is April 1 2009
so
SELECT DATENAME(MONTH, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 109), 9)
Result is:
select datename(DAY,GETDATE()) +'-'+ datename(MONTH,GETDATE()) +'- '+
datename(YEAR,GETDATE()) as 'yourcolumnname'