Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    SQL Server saving time as a decimal? How do I convert it to timestamp?

    Hey so I'm working with some tables from an SQL server and I can't change the format but I need to be able to read it and store it.
    Currently it stores the time like "10.83973". That is an example pulled from the table. In excel I can convert it to 8:09:13PM but I need to know how to do it in Access and how to reverse it so I can store the current time as well.
    Access says it is a short text field and I do not have access to the SQL database easily (I would have to ask my boss to get whatever info I needed and he is usually very busy)



    So basically I am looking to understand and be able to convert 10.83973 to 8:09:13PM in Access/VBA (Assuming that is correctly converted which it seemed to be)

    Appreciate the help and apologies for weird wording, I wasn't sure how to explain it really.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I suspect that the field is a DateTime2 data type on the SQL server, and although later versions of Access are supposed to support them they don't do a great job of it.
    Is it just the time or does it have a date component as well?

    I think I have answered that myself- in the immediate window :
    Code:
    ?cdate("10.83973")
    09/01/1900 20:09:13
    So you can use
    Code:
    ?timevalue(cdate("10.83973"))
    20:09:13
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
    ?timevalue(cdate("10.83973"))
    8:09:13 PM
    edit: ah, Minty beat me.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    date and time is stored as a decimal - the bit before the dp is the number of days since 30/12/1899, the bit after is the time expressed as the number of seconds since midnight divided by 86400 (the number of seconds in a day)

    So I'm not sure what the 10 is in your example so leaving it out and using the cdate function returns

    ?cdate(0.83973)
    20:09:13

    or if you want it formatted as a string

    ?format(0.83973,"hh:nn:ss AM/PM")
    08:09:13 PM


    note with the 10, cdate produces a date

    ?cdate("10.83973")
    09/01/1900 20:09:13

    The format function will return just the time element as before

    ?format("10.83973","hh:nn:ss AM/PM")
    08:09:13 PM

    so you really need to understand what the 10 is all about

  5. #5
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    I suspect that the field is a DateTime2 data type on the SQL server, and although later versions of Access are supposed to support them they don't do a great job of it.
    Is it just the time or does it have a date component as well?

    I think I have answered that myself- in the immediate window :
    Code:
    ?cdate("10.83973")
    09/01/1900 20:09:13
    So you can use
    Code:
    ?timevalue(cdate("10.83973"))
    20:09:13
    This seems to work perfectly fine! Why did you have a question mark infront of it though?
    Also how can I convert the current time into that format? As this form will be marking a step complete and I want it to save the current time.

    Thanks so much!

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    Why did you have a question mark infront of it though?
    that's what you use when you want to see a value or the result of a calculation/use of a function in the immediate window

    Still think you need to know what the 10 is all about per post #4

    how can I convert the current time into that format?

    you use the time function
    ?time
    19:54:54

    Edit: or the now function
    ?now
    27/10/2022 19:56:28

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by CJ_London View Post
    that's what you use when you want to see a value or the result of a calculation/use of a function in the immediate window

    Still think you need to know what the 10 is all about per post #4

    The 10 is because it uses DateTime2 as Minty said I presume because I get seemingly accurate times when I use it.

    Quote Originally Posted by CJ_London View Post
    you use the time function
    ?time
    19:54:54

    Edit: or the now function
    ?now
    27/10/2022 19:56:28
    I need to convert the current time into the DateTime2 Format though so it can be re-entered into the SQL Db. How would I do that?

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    Unlike datetime, datetime2 stores the time first, then the date which is why I wonder about the 10 - strongly recommend you verify the time is correct by looking at the same value in the source system. I'm not saying it is wrong, but if it is it will be wrong in both access and excel. Not clear what 'Assuming that is correctly converted which it seemed to be' actually means, either because it looks like a time or you have check against the source system.

    I don't know how to convert datetime to datetime2, but given datetime 2 provides a higher level of accuracy than you can get with datetime then I suspect just return as a datetime value and sql server will do the conversion.

  9. #9
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    I would never rely on SQL server to do conversions automatically, even not from one date type to another. Use the cast or convert functions.

  10. #10
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by CJ_London View Post
    Unlike datetime, datetime2 stores the time first, then the date which is why I wonder about the 10 - strongly recommend you verify the time is correct by looking at the same value in the source system. I'm not saying it is wrong, but if it is it will be wrong in both access and excel. Not clear what 'Assuming that is correctly converted which it seemed to be' actually means, either because it looks like a time or you have check against the source system.

    I don't know how to convert datetime to datetime2, but given datetime 2 provides a higher level of accuracy than you can get with datetime then I suspect just return as a datetime value and sql server will do the conversion.
    I realize it has been a while but I just realized the other day that I can check the times through a separate management software. Though I cannot seem to find any info on how it saves it.
    Maybe knowing the time it should convert to would help us figure out this mystery?
    I have attached a picture so you can see that there is a separate field for dates as well.
    Click image for larger version. 

Name:	timefields.jpg 
Views:	15 
Size:	6.5 KB 
ID:	49052

    10.02702 = 8:12AM not sure how?

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    10.02702 = 8:12AM not sure how?


    not sure whether that means you are getting 8:12AM or should be getting 8:12AM

    I would expect it to be 00:38:55

  12. #12
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    It does equal 8:12AM. Here's a picture of the same record pulled up in the other program.
    Click image for larger version. 

Name:	Timeconvert.png 
Views:	14 
Size:	6.4 KB 
ID:	49053

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Well that scuppers the Idea that it is a datetime2 I think.
    From the immediate window;

    ? cdate("10.02702")
    09/01/1900 00:38:55

    as CJ stated that time portion (fractional part of the number) equates to 00:38:55 AM

    So maybe the 10. does have a significance we don't understand?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    the time value for 8:12AM is

    ?cdbl(#08:12:00#)
    0.341666666666667

    bears no relation to

    10.02702

    Going back to the original post, not clear what

    so I'm working with some tables from an SQL server
    means - are you linked to sql server, receiving a download as a .csv? or something else

    If you are linked then updating the ODBC driver might solve the problem - see this link as an example
    https://social.msdn.microsoft.com/Fo...databasedesign

    otherwise google something like 'datetime2 in Access'

    Edit: if you want the science behind how datetime and datetime2 vary in how they are determined
    https://towardsdatascience.com/datet...t-70e50ae2bab9

    SO might be possible to work it backwards, but I don't have the time right now

  15. #15
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    You could get 8 from 10 if 10 was octal. But the rest??
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-07-2022, 11:10 AM
  2. Replies: 7
    Last Post: 10-27-2021, 08:34 AM
  3. How to add timestamp when saving form
    By Bebobrokstedy in forum Forum Suggestions
    Replies: 7
    Last Post: 08-24-2020, 11:57 AM
  4. Replies: 1
    Last Post: 08-12-2014, 09:48 PM
  5. Convert decimal comma to decimal point
    By Hans Karlsson in forum Programming
    Replies: 3
    Last Post: 06-30-2014, 01:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums