TO_CHAR and BETWEEN Operator in my SQL Code

JrExceler

New Member
Joined
Nov 18, 2016
Messages
17
Hello Everyone,

I'm using SQL to get data from Access to my Excel Sheet, but I want to use the invoice date to have another filter criteria,
Do you know how can it be done?

I'm trying to convert the Invoice Date with:
" TO_CHAR(`Invoice Date`, 'YYYYMMDD')," & _
and then use BETWEEN in my Query,
however, it's giving me error.

¿is there any other way to get it?

This is my original Code that is Working,
But I cannot make it work with BETWEEN operator.

Any idea?


Code:
SQL0 = " SELECT " & _
        " Mat," & _
        " `Vol`," & _
        " Mon," & _
        " `Invoice Date`," & _
        " `Group`," & _
        " Dest," & _
        " source," & _
        " Sales"
SQL7 = " FROM `C:\Sales\NSalesAc.accdb`.NSales" & _
  " WHERE `Group` = '" & MODL & "' " & _
  " AND Dest = '" & MKT & "' "
     SQL = SQL0 + SQL7
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
What criteria are trying to set and how have you tried using TO_CHAR and BETWEEN?
 
Upvote 0
What criteria are trying to set and how have you tried using TO_CHAR and BETWEEN?

The first thing I tried was to change Date in the original format of: dd/mm/yyyy to the format YYYYMMDD


Code:
SQL0 = " SELECT " & _
        " Mat," & _
        " `Vol`," & _
        " Mon," & _
        " TO_CHAR (`Invoice Date`, 'YYYYMMDD'), " & _
        " `Group`," & _
        " Dest," & _
        " source," & _
        " Sales"
SQL7 = " FROM `C:\Sales\NSalesAc.accdb`.NSales" & _
  " WHERE `Group` = '" & MODL & "' " & _
 
  " AND Dest = '" & MKT & "' "

     SQL = SQL0 + SQL7

But it's giving syntax error,
Any idea how to solve this error? what am I doing wrong with the code?
 
Upvote 0
If anything you should probably use a format like yyyy-mm-yy.

Have you tried creating the query you want in Access itself?

If you can do that successfully you can get the proper SQL syntax from the SQL view of the query.

PS Pretty sure there's no TO_CHAR function in Access SQL. Also, you might want to avoid using 'Group' for a field name, could get confused with SQL's GROUP BY clause.
 
Upvote 0
Oops, that format should be yyyy-mm-dd.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,221,710
Messages
6,161,445
Members
451,706
Latest member
SMB1982

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