views:

34

answers:

2

I have the following integer type values in a SQL script: @year, @month, @day. Now I want to convert those into a datetime value. Should be easy, right?

Well, I just went through the SQL documentation and was very surprised that I couldn't find any way to do this, other than converting to a string and then to a datetime.

declare @dt datetime
set @dt= convert(varchar,@year)+'/'+convert(varchar,@month)+'/'+convert(varchar,@day)

This is horrible! Surely there has to be a way to convert straight from the int values to the datetime?

+1  A: 

Not out of the box, but you could create a UDF that does it, for example:

create function ints2date (@year int, @month int, @day int) 
returns datetime 
as begin
    declare @foo varchar (10)

    set @foo = convert (varchar, @year) + '-' +
               convert (varchar, @month) + '-' +
               convert (varchar, @day)
    return convert (datetime, @foo)
end
go               

select dbo.ints2date (2000,1,1)

You can also do it in a more convoluted (but probably slightly faster) way using dateadd/datepart. An example of this can be found at Create a date with T-SQL (stackoverflow.com).

ConcernedOfTunbridgeWells
Seriously?? MS *really* didn't provide a native method for creating a date from the date parts? And you have to turn the integers into strings before you can create a date? I'm absolutely **horrified**! Yes, given the facts, using a UDF would be the way to go - but I am just totally speechless at the stupidity of it all. AFAI am concerned, that is a big BUG in SQL Server.
Shaul
No, it doesn't do this - a bit of a minor annoyance with T-SQL. Note that converting dates is easiest if you construct them in the form YYYY-MM-DD. To be fair, I don't think TO_DATE() in Oracle is appreciably more convenient.
ConcernedOfTunbridgeWells
OK, it wasn't the answer I was hoping for, but being accurate, and since you got in the first answer, I'll give you the credit.
Shaul
+1  A: 

You can do it without converting to string like this. It's not a single function, which would be nice, but it works:

DECLARE
    @year   SMALLINT,
    @month  TINYINT,
    @day    TINYINT,
    @d      DATETIME

SET @year = 2010
SET @month = 6
SET @day = 23
SET @d = '1900-01-01'

SELECT
    DATEADD(dy, @day - 1, DATEADD(mm, @month - 1, DATEADD(yy, @year - 1900, @d)))
Tom H.
Yes, but it's still relying a string conversion to create the original value of `@d`...
Shaul
@d is a datetime. If it bothers you that it has what looks like a string literal on the other side of the assignment operator then you can remove it completely from the script and just put 0 in its place in the final function.
Tom H.