"Erland Sommarskog" <so****@algonet.se> wrote:
Nikola (ni*****@hotmail.com) writes: AM Example:
declare @DI integer; declare @DD datetime
set @DI = cast(cast('3/12/2003 11:34:02 AM' as datetime) as integer)
set @DD = cast (@DI as datetime)
print @DI; print @DD
Result:
37690
Mar 12 2003 12:00AM
PM Example:
declare @DI integer; declare @DD datetime
set @DI = cast(cast('3/12/2003 11:34:02 PM' as datetime) as integer)
set @DD = cast (@DI as datetime)
print @DI; print @DD
Result:
37691
Mar 13 2003 12:00AM
Now, this is not a big problem if I knew that this is how it is
supposed to work. Is this how SQL Server is supposed to work?
Apparently, SQL Server rounds to the nearest wholest int. I wouldn't
say this makes much sense to me.
Then again, I have to admit that I don't really see the point with
converting datetime values to integer.
In any case, the workaround should be simple, first chop of the
time portion.
VB6 will allow a similar translation and it has the same problem: a real
number is returned where the fractional (i.e. right of the decimal point)
part represents the time. So, converting from datetime to an int carries a
hidden conversion that rounds to get the integer. Check this out (I used
money, although I assume float or real would suffice).
declare @d datetime
declare @n money
set @d = '3/12/2003'
set @n = convert(money, @d)
print convert(varchar, @n) + ' - ' + convert(varchar, @d)
set @d = '3/12/2003 11:34 AM'
set @n = convert(money, @d)
print convert(varchar, @n) + ' - ' + convert(varchar, @d)
set @d = '3/12/2003 11:34 PM'
set @n = convert(money, @d)
print convert(varchar, @n) + ' - ' + convert(varchar, @d)
set @d = '3/13/2003'
set @n = convert(money, @d)
print convert(varchar, @n) + ' - ' + convert(varchar, @d)
Craig