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.