How Convert specific String To Month Name - sql

Example:
my database table data is 201201 which i want to display as 2012 DECEMBER . Please advice me. SQL 2005 Environment.

How about this?
DECLARE #DT DATETIME
SET #DT = CONVERT(DATETIME,'201201' + '01')
SELECT CAST(YEAR(#DT) AS VARCHAR(4)) + ' ' + DATENAME(MM, #DT) AS [Month YYYY]

Related

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 to Convert int value 20140401 to 'April 2014' in SQL Server 2008

This is what I tried .....
declare #date varchar(8)
set #date = (select max(convert(varchar(10), fileextractperiod))
from [info-CentralReturns-DEV].CentralReturns.FactCancer)
select CONVERT(varchar(10), cast(#date as DATE), 103)
Resulting output: 01/04/2014
But I need output as April 2014
SQL Server 2008 only supports the limited set of system-provided styles for CONVERT - if none of those match your needs, you'll need to handle it yourself. SQL Server 2012 has a FORMAT function for this purpose..
So in SQL Server 2008, you could create a function like this:
CREATE FUNCTION dbo.FormatDateMonthYear(#input DATE)
RETURNS VARCHAR(50)
AS BEGIN
DECLARE #Year INT = YEAR(#input)
DECLARE #MonthName VARCHAR(15) = DATENAME(MONTH, #input)
DECLARE #Result VARCHAR(50)
SET #Result = #MonthName + ' ' + CAST(#Year AS VARCHAR(4))
RETURN #Result
END
and then call it to get the format you need:
SELECT dbo.FormatDateMonthYear('2014-04-22')
and you should get
April 2014
SELECT
CONVERT(CHAR(4), #date, 100) + CONVERT(CHAR(4), #date, 120)
SELECT
DateName( month , DateAdd( month , cast(SUBSTRING(#date, 5, 2) as int), -1 ) )
+ ' ' + SUBSTRING(#date, 1, 4)
Select DATENAME(MONTH,#date)+' '+CONVERT(VARCHAR(4),YEAR(#Date))

convert datetime to display full month

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

How to flip month and day in a datetime?

How can I flip "2012-12-01 12:33:00.0" to become "2012-01-12 12:33:00.0"? I had tried "select convert(varchar(50), convert(datetime, log_date, 103), 121)" and used both 101 and 103 and still not able to flip it.
The database is MS SQL
DECLARE #date DATETIME
SET DATEFORMAT ydm
SET #date = '2012-12-01 12:33:00.0'
SELECT #date
SET DATEFORMAT ydm will give you the result in your required format.
Other Option
UPDATE Table_Name
SET COLUMN_NAME= convert(varchar(20), convert(Date, #date, 101)) + convert(varchar(30),convert(time, #date))
WHERE
OR
select convert(datetime, convert(varchar(100), #date), 20)
You could just perform some string manipulation (NOTE: Untested code)
UPDATE table
SET date=
LEFT(CONVERT(VARCHAR(50),date,128),4) -- '2012'
+SUBSTRING(CONVERT(VARCHAR(50),date,128),7,3) -- '-01'
+SUBSTRING(CONVERT(VARCHAR(50),date,128),4,3) -- '-12'
+RIGHT(CONVERT(VARCHAR(50),date,128),LEN(CONVERT(VARCHAR(50),date,128))-10) -- ' 12:33:00.0'
You could use the Format function to swap day and month, while keeping the year and hour:
update MyTable
set [date] =
FORMAT([date],'yyyy') + '-' +
FORMAT([date],'dd') + '-' +
FORMAT([date],'MM') + ' ' +
FORMAT([date],'hh:mm:ss')
from MyTable

Date Format conversion in SQL

I want to convert date format from 01/09 to January 2009 , 09/03 to September 2003 etc. Is this possible in SQL? Please let me know if there is a API for the same.
if you have a DateTime column in your table, it's possible
SELECT DATENAME(MM, YOUR_DATE_COLUMN) + ' ' + CAST(YEAR(YOUR_DATE_COLUMN) AS VARCHAR(4)) AS [Month YYYY]
http://www.sql-server-helper.com/tips/date-formats.aspx
You should look here.
It's rather simple.
You should first convert it to a datetime. Then you can easily apply any formating when you read it later.
declare #d varchar(10);
set #d = '01/09'
select
--cast(#d as datetime) as d1, --syntax error converting char string
cast('20' + right(#d, 2) + '-' + left(#d, 2) + '-01' as datetime) as d2
then convert it to mmm yyyy using rm's answer
select datename(month, GETDATE()) + ' '+ substring(convert(varchar, GETDATE(), 100),8,4)