extract date from SQL server Month Name is wrong

baha17

Board Regular
Joined
May 12, 2010
Messages
183
Dear All,
Lately I have been posting some question but could not get any answer. I am not sure that my questions were not clear or not but I managed to fix my problems by myself. However, I don’t know why I could not get any help. Please let me know if there is something wrong with my post this time. Here is my problem.

I wrote a code that extracts data from sql server. Everything works fine except this funny issue. Whenever the date is 30 Dec 2014 or 31 Dec 2014 the month appears as January. How can this happen? Any idea? For security issue I changed my server name and the database name. Below is my code:
Code:
Sub GetingItFromServer()
Dim DBFullName, TableName As String
Dim TargetRange As Range
Dim Conn As ADODB.Connection, intColIndex As Integer
Dim cel As Range
Dim TD As Long
Dim qdate1 As Double
Dim qdate2 As Double
Dim LastRow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
qdate1 = Range("trddate1").Value
qdate2 = Range("trddate2").Value
'MsgBox qdate1 & " " & qdate2
'Exit Sub
    Sheets("TableData").Range("A2:J1048576").ClearContents
    Sheets("TableData").Select
    Columns("A:H").AutoFilter
    Range("A2").Select
    Selection.Activate
    Set TargetRange = Range("A2")
    Set Conn = New ADODB.Connection
     Conn.Open "driver={SQL Server};" & _
    "server=TrVST1;database=OverAllData;"
        Set RecSet = New Recordset
    RecSet.Open "SELECT   hist_shift.table_id,hist_shift.game_date, {fn MONTHNAME(hist_shift.game_date)}   FROM hist_shift hist_shift " & _
    "WHERE hist_shift.game_date>='" & qdate1 & "' And hist_shift.game_date<='" & qdate2 & "' ORDER BY hist_shift.pit_name", Conn, , , adCmdText
        TargetRange.CopyFromRecordset RecSet
    RecSet.Close
    Set RecSet = Nothing
    Conn.Close
    Set Conn = Nothing
LastRow = Sheets("TableData").Range("A" & Sheets("TableData").Rows.Count).End(xlUp).Row
    With ActiveWorkbook.Worksheets("TableData").Sort
        .SetRange Range("A2:J" & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With
        Columns("B:B").NumberFormat = "[$-409]d-mmm-yy;@"
    Columns("A:H").AutoFilter
End Sub
On the other hand I also try this which is only to test the month name of 31 Dec 2014, but I still got January:
Code:
 RecSet.Open "SELECT   hist_shift.table_id,hist_shift.game_date, {fn MONTHNAME(42004)}   FROM hist_shift hist_shift
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I think my problem is the date data in number value so I need to convert that into date first. Therefore I altered the code as below but still not correct.
Code:
{fn MONTHNAME(CONVERT(DATETIME,hist_shift.game_date))}
 
Upvote 0
After a long research I found out there is a 2 days difference between sql and excel starting date. Please reply if I am wrong.
Thx
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top