Results 1 to 8 of 8
  1. #1
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19

    MS Access - formatting Short Text to Datetime with CDate - problem

    Hi,



    I try to formatting TEXT into DATE.
    I have a column F9 with date but as "Short Text".
    Example 11.03.2022 23:59:03
    I want to convert it to datetime in my query.

    Code:
    SELECT *
    FROM myTable
    WHERE CDate(Format(F9, "dd.MM.yyyy HH:mm:ss")) > #20.03.2022 11:12:13#
    here: Syntax error in date in query expression CDate(Format(F9, "dd.MM.yyyy HH:mm:ss")) > #20.03.2022 11:12:13#




    Code:
    SELECT CDate(Format(F9, "dd.MM.yyyy HH:mm:ss")) as test
    FROM myTable
    here show all results as #error

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Convert to date, THEN format it.

    format(cdate(field),”dd/mm/yyyy”)

  3. #3
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19
    Hi,
    I get #ERROR as result :C
    I have no idea why, I tried many combinations

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,939
    it may be to due with your date structure of dd.MM.yyyy HH:mm:ss

    depends on your local area for dates but usually you would expect to see

    dd/mm/yyyy hh:nn:ss
    using / in the datepart and nn for the minutes

    CDate will reference your local settings for what it expects to look like a date

    suggest try

    format(cdate(replace(field,".","/",1,2)),”dd/mm/yyyy”)

    also need to clarify if
    11.03.2022 is 11th March or 3rd November as sql always wants the format US style of mm/dd/yyyy or the sql style of yyyy-mm-dd

  5. #5
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19
    Quote Originally Posted by Ajax View Post
    it may be to due with your date structure of dd.MM.yyyy HH:mm:ss

    depends on your local area for dates but usually you would expect to see

    dd/mm/yyyy hh:nn:ss
    using / in the datepart and nn for the minutes

    CDate will reference your local settings for what it expects to look like a date

    suggest try

    format(cdate(replace(field,".","/",1,2)),”dd/mm/yyyy”)

    also need to clarify if
    11.03.2022 is 11th March or 3rd November as sql always wants the format US style of mm/dd/yyyy or the sql style of yyyy-mm-dd

    Hi, also I thins its work, if I try:

    SELECT format(cdate(replace(Field16,".","/",1,2)),"dd/mm/yyyy")
    FROM myTable

    I become correctly result.


    But how should I use it in WHERE clausule?

    SELECT * FROM myTable
    WHERE format(cdate(replace(Field16,".","/",1,2)),"dd/mm/yyyy") > #20/03/2022#
    I become:
    Data type mismatch in criteria

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    If it's true that Format function converts data to a string, then it looks to me like you're converting to a date, then formatting as a string and then trying to compare that string to a date.
    Try removing the Format part, as I suspect even using format function on #20/03/2022# is not going to work.

    EDIT
    - text date field, query will return a value without using criteria. That value is text (it is left aligned in the query); also tested with TypeName function.
    - tried ever variation I could think of on the criteria: Cstr(), CDate with and without Format. I guess at some point the criteria is not meshing with the CDate in the expression regardless of the fact that the end result of using Format is a string. Can't even compare the Format result against a string date.

    So maybe the question is, why not stick with string date values, or convert them to dates in the table?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,939
    this works for me

    Code:
    SELECT PK, CDate(Replace([dateDT],".","/",1,2)) AS Expr1
    FROM Table1
    WHERE (((CDate(Replace([dateDT],".","/",1,2)))>#3/25/2022#))
    with this data
    pk dateDT
    1 11.03.2022 23:59:03
    2 12.03.2022 23:59:03
    3 11.04.2022 23:59:03

    returns
    PK Expr1
    3 11/04/2022 23:59:03


    or use the datevalue function instead will exclude the time element

    Code:
    SELECT PK, datevalue(Replace([dateDT],".","/",1,2)) AS Expr1
    FROM Table1
    WHERE (((datevalue(Replace([dateDT],".","/",1,2)))>#3/25/2022#))
    returns
    PK Expr1
    3 11/04/2022


    as I mentioned before with sql date strings have to be in the format mm/dd/yyyy or yyyy-mm-dd so

    WHERE format(cdate(replace(Field16,".","/",1,2)),"dd/mm/yyyy") > #20/03/2022#

    will work (once your remove the format) for 20/03/2022 because sql is clever enough to know there are not 20 months in the year. but if the value was say 10/03/2022, that will be interpreted as 3rd October, not 10th March

  8. #8
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19
    its work thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 11-04-2019, 06:53 PM
  2. Replies: 3
    Last Post: 11-14-2018, 01:20 PM
  3. Replies: 2
    Last Post: 07-14-2016, 09:23 PM
  4. Conditional Formatting on datetime field
    By Delta729 in forum Access
    Replies: 3
    Last Post: 05-06-2015, 03:37 PM
  5. Convert Short text column to date ms access
    By SameerSarswat in forum Access
    Replies: 3
    Last Post: 10-09-2014, 10:42 AM

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