0 votes
1 view
in SQL by (22.4k points)

I am trying to convert a date with individual parts such as 12, 1, 2007 into a datetime in SQL Server 2005. I have tried the following:

CAST(DATEPART(year, DATE)+'-'+ DATEPART(month, DATE) +'-'+ DATEPART(day, DATE) AS DATETIME)

but this results in the wrong date. What is the correct way to turn the three date values into a proper datetime format.

1 Answer

0 votes
by (40.3k points)

Let's assume d, m, and y all as int and try the below code:

CAST(CAST(d AS varchar) + '-' +CAST(m AS varchar) + '-' +CAST(y AS varchar) AS DATETIME)

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...