+ Reply to Thread
Results 1 to 3 of 3

Thread: Date Formatting In SQL

  1. #1
    Registered User
    Join Date
    03-16-2006
    Posts
    76

    Date Formatting In SQL

    I have the following code which works fine, with the exception of the formatting of the date (English formatting).

    I have tried changing the formatting, but if the date is, for example, 09/12/2011 (9th December) it will input 12/09/2011.
    If the date is above 11/12/2011 (11th December) it enters the date fine.

    Does anyone know of a fix for this.

    Sub StartEndDates()
    
    Dim sqlQry As String
    Dim yesterdayDate As String
    Dim rsTemp As New ADODB.Recordset
    
        sqlQry = "UPDATE WeekID_tbl SET WeekID_tbl.[Date] = Format(Now()-1,'dd/mm/yyyy')"
        DoCmd.RunSQL sqlQry
    
        'Gets the date for the first day of the reporting week
        rsTemp.Open "SELECT Min(Calendar_tbl.[Date]) AS MinOfWeekDate " & _
              "FROM Calendar_tbl INNER JOIN WeekID_tbl ON Calendar_tbl.[WeekID] = WeekID_tbl.[WeekID] " & _
              "HAVING Calendar_tbl.[WeekID]=WeekID_tbl.[WeekID];", _
              CurrentProject.Connection, adOpenStatic, adLockReadOnly
    
        sqlQry = "UPDATE WeekID_tbl SET WeekID_tbl.[FirstDayOfWeek] = Format(#" & rsTemp!MinOfWeekDate & "#,'dd/mmm/yy');"
        DoCmd.RunSQL sqlQry
    
        rsTemp.Close
    
        'Gets the date for the last day of the reporting week
        rsTemp.Open "SELECT Max(Calendar_tbl.[Date]) AS MaxOfWeekDate " & _
              "FROM Calendar_tbl INNER JOIN WeekID_tbl ON Calendar_tbl.[WeekID] = WeekID_tbl.[WeekID] " & _
              "HAVING Calendar_tbl.[WeekID]=WeekID_tbl.[WeekID];", _
              CurrentProject.Connection, adOpenStatic, adLockReadOnly
    
        sqlQry = "UPDATE WeekID_tbl SET WeekID_tbl.[LastDayOfWeek] = Format(#" & rsTemp!MaxOfWeekDate & "#,'ddd/mmm/yy');"
        DoCmd.RunSQL sqlQry
    
        rsTemp.Close
    
    End Sub

  2. #2
    Cheeky Forum Moderator Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2003, 2007, 2010
    Posts
    3,686

    Re: Date Formatting In SQL

    Excel is trying to interpret the data it is receiving.
    Your region settings for dates are probably mm/dd/yyyy, so Excel will assume the 1st date section is the month.

    Try creating the date in this format: mm/dd/yyyy
    Then format the dates into dd/mm/yyyy format in Excel.

    Does that help?
    Regards,

    Ron
    Microsoft MVP - Excel
    (Oct 2006 - Sep 2012)

    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    03-16-2006
    Posts
    76

    Re: Date Formatting In SQL

    Thanks Ron, you gave me an idea, it's Access i'm using by the way.
    I sort of did the opposite, left the data as it is and changed the formatting and now it inserts it in 'rest of the world' format.

    sqlQry = "UPDATE WeekID_tbl SET WeekID_tbl.[FirstDayOfWeek] = Format(#" & rsTemp!MinOfWeekDate & "#,'mm/dd/yy');"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0