Results 1 to 4 of 4
  1. #1
    Garett is offline Novice
    Windows XP Access 2003
    Join Date
    Jun 2022
    Posts
    1

    How to convert Date in SQL-query?

    Hi guys, need some help with SQL-query to *.mdb file. Trying to read date/time field but I need format DD.MM.YYYY but receiving YYYY-MM-DD HH:MM:SS This is my SQL-query to database from MFC (Visual C++):

    CRecordset recset2( &database );


    SqlString2 = "SELECT Main.[S/N], Employers.Employer, History.Date FROM Main INNER JOIN (Employers INNER JOIN History ON Employers.employer_code = History.employer_code) ON Main.device_code = History.device_code";
    SqlString2+=" WHERE [S/N]=";
    SqlString2+="'";
    SqlString2+=sSerial;
    SqlString2+="' ORDER BY History.Date";




    recset2.Open(CRecordset::forwardOnly,SqlString2,CR ecordset::readOnly);
    while( !recset2.IsEOF() )

    {
    recset2.GetFieldValue("Date",sDate);
    recset2.GetFieldValue("Employer",sEmpl);
    recset2.MoveNext();

    }


    recset2.Close();

    So I need to read all the values of "Date" field in the table "History" and convert it to DD.MM.YYYY format - how to? Thank you for support.

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,526
    import the date by chopping it up as a string before placing it into a Date Field:
    cvtStrDate()
    once its converted into a Date Type field, then you can display the format as :dd/mm/yyyy

    Code:
    public function cvtStrDate( pvDateFld) 
    dim vDate
    Code:
    if isnull(pvDateFld) then exit function
    y = left(pvDateFld,4)
    m = mid(pvDateFld,6,2)
    d = mid(pvDateFld,9,2)
    h = mid(pvDateFld,12,2)
    n = mid(pvDateFld,15,2)
    s = right(pvDateFld,2)
    vDate= m & "/" & d & "/" & y & " " & h & ":" & n & ":" & s
    cvtStrDate = cvDate(vDate)
    end  function
    

  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    BE aware that object names in Access should only be letters and/or numbers. NO punctuation or special characters.

    "[S/N]" is a poor field name be of the slash. "Date" is a reserved word AND a built in function.
    Plus "Date" is not very descriptive - Date of What? Hire date, Term date, Promotion date?

    Code:
    SqlString2 = "SELECT Main.[S/N], Employers.Employer, History.Date  FROM Main INNER JOIN (Employers INNER JOIN History ON  Employers.employer_code = History.employer_code) ON Main.device_code =  History.device_code";
    SqlString2+=" WHERE [S/N]=";
    SqlString2+="'";
    SqlString2+=sSerial;
    SqlString2+="' ORDER BY History.Date";

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Access would normally recognise that as a valid date format - so what is the actual issue?

    You could add a conversion field:

    Code:
    SqlString2 = "SELECT Main.[S/N], Employers.Employer, History.[Date] , CDate(History.[Date]) as MyDate FROM Main INNER JOIN (Employers INNER JOIN History ON  Employers.employer_code = History.employer_code) ON Main.device_code =  History.device_code";SqlString2+=" WHERE [S/N]=";
    SqlString2+="'";
    SqlString2+=sSerial;
    SqlString2+="' ORDER BY History.Date";
    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 ↓↓

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

Similar Threads

  1. Replies: 6
    Last Post: 04-06-2020, 04:31 AM
  2. Convert String to Date in Query
    By McArthurGDM in forum Access
    Replies: 15
    Last Post: 06-22-2015, 10:52 AM
  3. Replies: 3
    Last Post: 02-12-2015, 01:22 PM
  4. Replies: 2
    Last Post: 09-23-2014, 12:54 PM
  5. Query to convert String to Date??
    By taimysho0 in forum Programming
    Replies: 3
    Last Post: 06-04-2012, 04:48 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