简体   繁体   中英

TSQL - Convert string yyyy/day number to date

In T-SQL, I am trying to convert the following varchar into a date so I can see if that date is 90 or 180 days prior to today.

YYYY/(Day of the year)

For example: 2016/53 would be 2/22/2016.

If I ran this...

SELECT
[DAY] as 'YR/DAY'
,left([Day],4) as Year
,right([Day],LEN([Day])-CHARINDEX('/',[Day])) as Day
FROM 
DATE_TABLE

I would get this...

YR/DAY    Year  Day
2016/53   2016  53

I would like to get this...

YR/DAY    Year  Day  Date
2016/53   2016  53   2/22/2016

One way to do it:

DATEADD(DAY, [day] - 1, CAST([year] AS CHAR(4)) + '-01-01')

Added a missing ')'

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