Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k 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.7k 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)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...