SQL Server: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]
Solution 1:
In order to be independent of the language and locale settings, you should use the ISO 8601 YYYYMMDD
format - this will work on any SQL Server system with any language and regional setting in effect:
SELECT
CAST(
CAST(year AS VARCHAR(4)) +
RIGHT('0' + CAST(month AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(day AS VARCHAR(2)), 2)
AS DATETIME)
Solution 2:
Pure datetime solution, does not depend on language or DATEFORMAT, no strings
SELECT
DATEADD(year, [year]-1900, DATEADD(month, [month]-1, DATEADD(day, [day]-1, 0)))
FROM
dbo.Table
Solution 3:
You could convert your values into a 'Decimal' datetime and convert it then to a real datetime column:
select cast(rtrim(year *10000+ month *100+ day) as datetime) as Date from DateTable
See here as well for more info.
Solution 4:
SELECT CAST(CAST(year AS varchar) + '/' + CAST(month AS varchar) + '/' + CAST(day as varchar) AS datetime) AS MyDateTime
FROM table