Andy0692
Andy0692

Reputation: 35

Date issue convert in sql

I have a date field I.[Creation Date Key]. I am going to display this field in 3 ways. One in its original format of yyyy-mm-dd tt:tt:tt

I am also going to display it as YYYYMM by using the following:

CONCAT (YEAR(I.[Creation Date Key]) , SUBSTRING(CONVERT(nvarchar(6),(I.[Creation Date Key]), 112),5,2))

Output is YYYYMM

Is there any way I can display this as YYYYWW, with the following WW being the week of the year that it is? Any help would be hugely appreciated.

Upvotes: 0

Views: 61

Answers (2)

JamieD77
JamieD77

Reputation: 13949

year(I.[Creation Date Key]) * 100 + datepart(week, I.[Creation Date Key])

Upvotes: 1

Andy3B
Andy3B

Reputation: 454

DECLARE @Date datetime = GETDATE()

SELECT CONVERT(varchar(06), @Date, 112) * 100 + DATEPART(WEEK, @Date)

Upvotes: 0

Related Questions