472,981 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

Converting datetime to integer and back

Hi all,

I have a problem converting datetime to integer (and than back to
datetime).
Depending whether the time is AM or PM, same date is converted to two
different integer representations, which holds as true on reversal
back to datetime.

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?
Jul 20 '05 #1
3 118755
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.


--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
"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
Jul 20 '05 #3
Erland Sommarskog (so****@algonet.se) writes:
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.


Actually there is an even simpler workaround:

declare @d datetime
declare @i int

SELECT @d = '20020202 11:59:00'
SELECT @i = convert(float, @d)
SELECT @i

SELECT @d = '20020202 12:01:00'
SELECT @i = convert(float, @d)
SELECT @i

This works, because when convering from float to int, truncation occurs...

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wong | last post by:
CREATE PROCEDURE dbo.Synchronization_GetNewRecords ( @item varchar(50), @last datetime ) AS SET NOCOUNT ON
1
by: js | last post by:
I have tables with columns that stores datetime data in int format on SQL server 2000. For example, the datetime for '4/5/2004 00:00:00.000am' is stored as 1081180800. "4/4/2004 11:59:59.000pm'...
1
by: John Dann | last post by:
I need to convert some datetime data currently stored in .Net DateTime type to the older double style (for compatibility with a legacy control). Is there an intrinsic function to do this or, if...
3
by: Rich Robinson | last post by:
Hi, I have a web service method which takes a DateTime type as a parameter. The service is UK based, and the dates are passed in to the service in the UK format dd/MM/yyyy. On a recent...
2
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I have time in the following format... Tue, 8 May 2007 13:38:00 EDT How can I convert it DateTime? Convert.ToDateTime works fine if the Time Zone is GMT like Tue, 8 May 2007 13:38:00...
2
by: joyjignesh | last post by:
hi i have make a report with mshflexgrid. the report between two date. when i have written query .open"select * from tablename where t_date>=convert(datetime,' " & text1.text &"') and ...
1
by: Matt | last post by:
Hi all, So a lot of digging on doing this and still not a fabulous solution: import time # this takes the last_modified_date naive datetime, converts it to a # UTC timetuple, converts that...
7
by: tragic54 | last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
1
by: SnehaAgrawal | last post by:
Hi I get the error "Syntax error converting datetime from character string "for the following sql statement in Stored proc. SET @dynamicSQL =N'SELECT CUSTID,SHCodeLink FROM AccountMaster where...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.