简体   繁体   中英

How to change date format from yyyy-mm-dd to yyyymm?

Please help with converting my date code to give output in YYYYMM format.

select convert (date,DATEADD(YEAR, DATEDIFF(YEAR,0,GETDATE()) + 1, -1))  as lastDayOfPrevYear
SELECT DATEFROMPARTS ( DATEPART(yyyy, GETDATE()) - 1, 1, 1 ) as firstDayOfPrevYear
SELECT DATEFROMPARTS(YEAR(GETDATE()), 1, 1) as firstDayOfCurrentYear

Dates do not have a format, but you can create a string or even an int as yyyymm

Example

Select convert(varchar(6),getdate(),112)

Simply use the FORMAT function like this

SELECT
    MyDate = GETDATE(),
    mmddyyyy = FORMAT(GETDATE(),'MMddyyyy'),
    YYYYMM = FORMAT(GETDATE(),'yyyyMM')

SELECT CONVERT(varchar,GETDATE(),112); the answer is "201907"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM