Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to YYYYMM format

I want to select value = 201301

select getdate(), cast(datepart(year, getdate()) as varchar(4))+cast(datepart(MONTH, getdate()) as varchar(2)) 

it returns 20131

what is the normal way to do this?

like image 831
loviji Avatar asked Jan 08 '13 14:01

loviji


People also ask

How do I change a date format to Yyyymm?

To convert a normal Excel date into yyyymm format (e.g. 9/1/2017 > 201709), you can use the TEXT function. The TEXT function applies the number format you specify to a numeric value, and returns a result as text.

How do you convert date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].

How do I change the date format in Yyyymmdd in SQL?

mm/dd/yyyy corresponds to U.S. standard so if you convert to date using 101 value and then to varchar using 112 for ISO date get the expected result.


2 Answers

SELECT CONVERT(nvarchar(6), GETDATE(), 112) 
like image 144
Hamlet Hakobyan Avatar answered Oct 05 '22 06:10

Hamlet Hakobyan


SELECT LEFT(CONVERT(varchar, GetDate(),112),6) 
like image 26
Hogan Avatar answered Oct 05 '22 07:10

Hogan