473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem with date formatting in SQL command

Hello,
I work in an application in VBA/Access, which connects to an SQL
Server database via ODBC.

I am trying to run a SQL command to the SQL Server, but I have some
data problems :

My command is (in VBA) :

Dim dateTest as String, strSQL as String

dateTest = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now))

strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE WHERE ID_PROJECT =
1000 AND ID_CYCLE <>2 AND PRX_END_DATE >= '" & dateTest & "'*

when I run the command without the PRX_END_DATE test, everything is
OK. But if I run it the way it is written (with simple quotes
surrounding the dateTest value), I get a 3464 message from Access
("Data type mismatch in criteria expression")
If I run this command directly to the SQL Server by using the tools
from Entreprise Manager, there is no problem at all, even with the
PRX_END_DATE test (I just replace dateTest with its value).

So my question is : how should I format the SQL command in VBA to
allow the testing of PRX_END_DATE being >= to the current date,
without getting the 3464 error message ?

Thank you.

Oct 15 '07 #1
8 2780


Note that PRX_END_DATE is a DATETIME field in the PRX_TABLE (in SQL
Server)

Oct 15 '07 #2
nartla wrote:
Hello,
I work in an application in VBA/Access, which connects to an SQL
Server database via ODBC.

I am trying to run a SQL command to the SQL Server, but I have some
data problems :

My command is (in VBA) :

Dim dateTest as String, strSQL as String

dateTest = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now))

strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE WHERE ID_PROJECT =
1000 AND ID_CYCLE <>2 AND PRX_END_DATE >= '" & dateTest & "'*

when I run the command without the PRX_END_DATE test, everything is
OK. But if I run it the way it is written (with simple quotes
surrounding the dateTest value), I get a 3464 message from Access
("Data type mismatch in criteria expression")
If I run this command directly to the SQL Server by using the tools
from Entreprise Manager, there is no problem at all, even with the
PRX_END_DATE test (I just replace dateTest with its value).

So my question is : how should I format the SQL command in VBA to
allow the testing of PRX_END_DATE being >= to the current date,
without getting the 3464 error message ?

Thank you.
Is PRX_END_DATE a character string in the database? You are passing a
character string in the query. If it's a number you might want to do a
CLng(dateTest) and remove the quotes.
Oct 15 '07 #3

Format the dateTest variable to YYYY-MM-DD format before enclosing it
in the single quotes:

strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'*

If there's also a time value in dateTest, use "YYYY-MM-DD HH:NN:SS".

On Mon, 15 Oct 2007 16:18:35 -0000, nartla <br**********@altran.com>
wrote:
>Hello,
I work in an application in VBA/Access, which connects to an SQL
Server database via ODBC.

I am trying to run a SQL command to the SQL Server, but I have some
data problems :

My command is (in VBA) :

Dim dateTest as String, strSQL as String

dateTest = CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now))

strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE WHERE ID_PROJECT =
1000 AND ID_CYCLE <>2 AND PRX_END_DATE >= '" & dateTest & "'*

when I run the command without the PRX_END_DATE test, everything is
OK. But if I run it the way it is written (with simple quotes
surrounding the dateTest value), I get a 3464 message from Access
("Data type mismatch in criteria expression")
If I run this command directly to the SQL Server by using the tools
from Entreprise Manager, there is no problem at all, even with the
PRX_END_DATE test (I just replace dateTest with its value).

So my question is : how should I format the SQL command in VBA to
allow the testing of PRX_END_DATE being >= to the current date,
without getting the 3464 error message ?

Thank you.
Please Post Any Replies To This Message Back To the Newsgroup.
There are "Lurkers" around who can benefit by our exchange!
Oct 15 '07 #4
On 16 oct, 01:24, Chuck Grimsby <c.grim...@worldnet.att.net.invalid>
wrote:
Format the dateTest variable to YYYY-MM-DD format before enclosing it
in the single quotes:

strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'*

If there's also a time value in dateTest, use "YYYY-MM-DD HH:NN:SS".
Hello,

Thank you for your answer.

If I just modify the command the way you suggested it and run the app,
I got a message about 'overflow', obviously because dateTest =
"20071016"

So I tried to make the following changes :

dateTest = CStr(Year(Now)) & "-" & CStr(Month(Now)) & "-" &
CStr(Day(Now)) ' dateTest now is "2007-10-16"
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'

but i'm still having the same old 3464 error message.

It seems that Access still does not understand that the value after
"PRX_END_DATE >= '" is supposed to be a date, but I don't find a way
to make it work ...
I even tried with "PRX_END_DATE >= CAST(" & dateTest & " AS
DATETIME)" which is supposed to be the right syntax for SQL Server,
but still not working ...


Oct 16 '07 #5
On Oct 16, 5:31 am, nartla <bruno.bar...@altran.comwrote:
On 16 oct, 01:24, Chuck Grimsby <c.grim...@worldnet.att.net.invalid>
wrote:
Format the dateTest variable to YYYY-MM-DD format before enclosing it
in the single quotes:
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'*
If there's also a time value in dateTest, use "YYYY-MM-DD HH:NN:SS".

Hello,

Thank you for your answer.

If I just modify the command the way you suggested it and run the app,
I got a message about 'overflow', obviously because dateTest =
"20071016"

So I tried to make the following changes :

dateTest = CStr(Year(Now)) & "-" & CStr(Month(Now)) & "-" &
CStr(Day(Now)) ' dateTest now is "2007-10-16"
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'

but i'm still having the same old 3464 error message.

It seems that Access still does not understand that the value after
"PRX_END_DATE >= '" is supposed to be a date, but I don't find a way
to make it work ...
I even tried with "PRX_END_DATE >= CAST(" & dateTest & " AS
DATETIME)" which is supposed to be the right syntax for SQL Server,
but still not working ...
Surely
"PRX_END_DATE >= CAST(" & dateTest & " AS
DATETIME)"
would generate a syntax error, or ask SQL to Cast the integer 20071016
as a date, and something like

CAST ('2007-10-16' AS DateTime)

or

CAST ('" & dateTest & "' AS DateTime)
single quote, double quote & dateTest & double quote, single quote
would be needed?

I use CAST ('2007-10-16' AS DateTime) extensively with no problems.
Then again I don't use ODBC, so this may not be applicable.


Oct 16 '07 #6
On 16 oct, 12:41, lyle <lyle.fairfi...@gmail.comwrote:
On Oct 16, 5:31 am, nartla <bruno.bar...@altran.comwrote:


On 16 oct, 01:24, Chuck Grimsby <c.grim...@worldnet.att.net.invalid>
wrote:
Format the dateTest variable to YYYY-MM-DD format before enclosing it
in the single quotes:
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'*
If there's also a time value in dateTest, use "YYYY-MM-DD HH:NN:SS".
Hello,
Thank you for your answer.
If I just modify the command the way you suggested it and run the app,
I got a message about 'overflow', obviously because dateTest =
"20071016"
So I tried to make the following changes :
dateTest = CStr(Year(Now)) & "-" & CStr(Month(Now)) & "-" &
CStr(Day(Now)) ' dateTest now is "2007-10-16"
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'
but i'm still having the same old 3464 error message.
It seems that Access still does not understand that the value after
"PRX_END_DATE >= '" is supposed to be a date, but I don't find a way
to make it work ...
I even tried with "PRX_END_DATE >= CAST(" & dateTest & " AS
DATETIME)" which is supposed to be the right syntax for SQL Server,
but still not working ...

Surely
"PRX_END_DATE >= CAST(" & dateTest & " ASDATETIME)"

would generate a syntax error, or ask SQL to Cast the integer 20071016
as a date


You should remember that in my case, dateTest is defined as a String
variable.

Oct 16 '07 #7
On 16 oct, 13:36, nartla <bruno.bar...@altran.comwrote:
On 16 oct, 12:41, lyle <lyle.fairfi...@gmail.comwrote:


On Oct 16, 5:31 am, nartla <bruno.bar...@altran.comwrote:
On 16 oct, 01:24, Chuck Grimsby <c.grim...@worldnet.att.net.invalid>
wrote:
Format the dateTest variable to YYYY-MM-DD format before enclosing it
in the single quotes:
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'*
If there's also a time value in dateTest, use "YYYY-MM-DD HH:NN:SS".
Hello,
Thank you for your answer.
If I just modify the command the way you suggested it and run the app,
I got a message about 'overflow', obviously because dateTest =
"20071016"
So I tried to make the following changes :
dateTest = CStr(Year(Now)) & "-" & CStr(Month(Now)) & "-" &
CStr(Day(Now)) ' dateTest now is "2007-10-16"
strSQL = "SELECT ID_PRX, PRX_LIB FROM PRX_TABLE " & _
"WHERE ID_PROJECT = 1000 AND ID_CYCLE <>2 AND " & _
"PRX_END_DATE >= '" & Format(dateTest, "YYYY-MM-DD") & "'
but i'm still having the same old 3464 error message.
It seems that Access still does not understand that the value after
"PRX_END_DATE >= '" is supposed to be a date, but I don't find a way
to make it work ...
I even tried with "PRX_END_DATE >= CAST(" & dateTest & " AS
DATETIME)" which is supposed to be the right syntax for SQL Server,
but still not working ...
Surely
"PRX_END_DATE >= CAST(" & dateTest & " ASDATETIME)"
would generate a syntax error, or ask SQL to Cast the integer 20071016
as a date

You should remember that in my case, dateTest is defined as a String
variable.- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Hello,

I finally made it work : since my SQL command is being launched from
an Access VBA application (to a SQL Server), the syntax I have to use
is the following :

SELECT... WHERE ... AND PRX_END_DATE >= #" & Format(Now(), "dd/mm/
yyyy") & "#"

It seems that using # before and after the date is a specificity of
Access syntax.

Thank you to all those who tried to help me !


Oct 16 '07 #8
nartla <br**********@altran.comwrote in
news:11**********************@t8g2000prg.googlegro ups.com:
You should remember that in my case, dateTest is defined as a String
variable.
VBA would know that. I would know that. You would know that. T-SQL would
not know that.

--
lyle fairfield
Oct 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JS | last post by:
What I have is a date that I'd like to format to keep it together on one line using non-breaking spaces. I started with: date ("l&nbsp;F&nbsp;j,&nbsp;Y", $unixtime); and soon realized that...
2
by: Sara | last post by:
The problem: Conditional formatting bold, red when field Value < date() sets the field background to white - always - whether condition is met or not. I want the field unfilled and just red/bold...
12
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
2
by: Thomas Beyerlein | last post by:
I am binding dates to a textbox, the date is stored in SQL in a datetime field. When it gets bound it turns it into a long date (Sunday, Dec. 25 2005), in SQL when viewing the table it views as a...
6
by: Piotr | last post by:
Hi, I have following problem: I use a form in excel to send data into mysql server, everything is ok unless I have to deal with decimals or data fields, this simple are not recognized. For...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
21
by: Darin | last post by:
I have an applicatoin that works 100% perfect when running on a machine setup for English (United States), but when I change it to Spanish (Mexico), the dates start giving me fits. THe reason is...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
6
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.