Domanda

I am currently trying to turn a Datetime field into a Year + Week field. what i have is the following

    CreateDate
------------------------
2012-08-15 07:22:56.000
2012-11-16 10:10:00.000
2013-01-22 08:47:37.000
2013-01-22 08:55:18.000
2013-01-22 09:00:38.000
2013-01-29 16:26:25.000

and i want this

 Weekstamp
------------------------
2013-32
2013-33
2013-34
2013-35
2013-36
2013-37
2013-45

can anybody Show me how this is done ? Thanks a lot in advance.

È stato utile?

Soluzione

Use this code

        select (cast(Year(YourDateTimeColumn) as varchar(4))+'-'+cast(datepart(wk,YourDateTimeColumn) as varchar(2))) Weekstamp from YourTableName

Altri suggerimenti

Thanks for the Answers, here is a slightly better Approach that i've found.

convert(varchar, year(CreateDate)) + '-' +right('0' + convert(varchar, datepart(iso_week, CreateDAte)),2) as Weekstamp
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top