Student
Student

Reputation: 91

Converting int to Date

How to convert to date in sql Server when i have data type int in my database?

enter image description here

For Example : 5th day in 4th week of 10th month in 2016

Upvotes: 0

Views: 115

Answers (2)

Sean Lange
Sean Lange

Reputation: 33581

This is a truly horrible way to store date information. It is a serious pain to work with and nearly impossible to validate. You really should use the native date datatype for this type of thing.

This produced the desired output for your sample. I think this is what you are looking for.

create table #SomeDate
(
    MyDay int
    , MyWeek int
    , MyMonth int
    , MyYear int
)

insert #SomeDate
select 5, 4, 10, 2016

select dateadd(day, s.MyDay + 1, dateadd(week, s.MyWeek - 1, dateadd(month, s.MyMonth - 1, dateadd(year, s.MyYear - 1900, 0))))
from #SomeDate s

drop table #SomeDate

Upvotes: 0

Pரதீப்
Pரதீப்

Reputation: 93754

Use can DATEFROMPARTS function

Select DATEFROMPARTS(Year,Month,Day) as Dates
From yourtable

If you are using less then 2012 then

Select cast(cast(year as char(4))+'-'+cast(month as varchar(2))+'-'+cast(day as varchar(2)) as date) as dates
From yourtable

Upvotes: 3

Related Questions