views:

102

answers:

1

In Sql Server 2008, they added new DATE and TIME datatypes, complimenting DATETIME.
I wanted to combine a DATE and a TIME to a DATETIME, and thought maybe the obvious would work, and I could do

SELECT DATEFLD + TIMEFLD FROM MYTABLE

and DATE + TIME would return the corresponding DATETIME. Unfortunately, that's a little too obvious, and did not work. So I am wondering, does Sql Server have any way to override operators, to create an override for "+" to handle DATE + TIME => DATETIME .

+1  A: 

CAST to smalldatetime first

DECLARE @d date, @t time

SELECT @d = getdate(), @t = getdate()

SELECT cast(@d as smalldatetime) + cast(@t as smalldatetime)

I can't recall where I saw this and I don't remember why. sorry

SQL does not provide overloading, luckily...

gbn
Interesting, that does work, though it seems a little odd. I also get conversion errors for some values... which I'm not that interested in tracking down right now. But it's interesting to know this works under most conditions. Thanks for the info!
eidylon

related questions