A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Date format in SQL



 
 
Thread Tools Display Modes
  #1  
Old May 19th, 2005, 02:59 AM
Dylan Moran
external usenet poster
 
Posts: n/a
Default Date format in SQL

I want to create a from which takes the user input date from a text box,
assigns it to a variable (using VBA) and then uses that variable to from part
of my WHERE clause in an SQL query (not QBE Grid query) and then execute that
SQL query.

I have done similar things with text before but not dates.

The date format on the form I want to use is DD/MM/YYYY.
I believe that SQL queries in MS Access are formatted in MM/DD/YYYY.

I was wondering if you know if I will experience any trouble with the date
format, and if you have any tips for this (or the process as a whole).

An example query at the moment using just hard dates, is as follows.
lic_grant (is licence granted date)
lic_sur (is licence surrendered date)


SELECT tblCustomer.company, tblCustomer.lic_sur, tblCustomer.lic_grant,
FROM tblCustomer
WHERE (((tblCustomer.lic_sur) Is Null) AND
((tblCustomer.lic_grant)=#6/30/2005#)) OR
(((tblCustomer.lic_sur)=#7/1/2004#) AND
((tblCustomer.lic_grant)=#6/30/2005#))
ORDER BY tblCustomer.lic_grant;

Your assistance is appreciated.

--
I may not know VBA inside out, but from the outside I am looking in.
Dylan Moran - Melbourne Australia
  #2  
Old May 19th, 2005, 06:12 AM
Marshall Barton
external usenet poster
 
Posts: n/a
Default

Dylan Moran wrote:

I want to create a from which takes the user input date from a text box,
assigns it to a variable (using VBA) and then uses that variable to from part
of my WHERE clause in an SQL query (not QBE Grid query) and then execute that
SQL query.

I have done similar things with text before but not dates.

The date format on the form I want to use is DD/MM/YYYY.
I believe that SQL queries in MS Access are formatted in MM/DD/YYYY.

I was wondering if you know if I will experience any trouble with the date
format, and if you have any tips for this (or the process as a whole).

An example query at the moment using just hard dates, is as follows.
lic_grant (is licence granted date)
lic_sur (is licence surrendered date)


SELECT tblCustomer.company, tblCustomer.lic_sur, tblCustomer.lic_grant,
FROM tblCustomer
WHERE (((tblCustomer.lic_sur) Is Null) AND
((tblCustomer.lic_grant)=#6/30/2005#)) OR
(((tblCustomer.lic_sur)=#7/1/2004#) AND
((tblCustomer.lic_grant)=#6/30/2005#))
ORDER BY tblCustomer.lic_grant;



You're right, date literals must be in an unambiguous format
(e.g. # yyyy-mm-dd#) or in USA format #m/d/yyyy#

The trick is to convert your date value variable to a
literal in the SQL string. Because of differing Windows
regional settings, you need to use the Format function to do
it:

tblCustomer.lic_sur = Format(dtSur, "\#m\/d\/yyyy\#")

The reason for the \ before the / is that an unescaped /
will be replaced by the regional setting for the date
separator character, which might not be acceptable in USA
format.

--
Marsh
MVP [MS Access]
  #3  
Old May 19th, 2005, 11:58 AM
JohnFol
external usenet poster
 
Posts: n/a
Default

Marshal, just remember #m/d/yyyy# is ambiguous for all non-us countries as
it's not clear if 01/02/2005 is Feb or March.



"Marshall Barton" wrote in message
...
Dylan Moran wrote:

I want to create a from which takes the user input date from a text box,
assigns it to a variable (using VBA) and then uses that variable to from
part
of my WHERE clause in an SQL query (not QBE Grid query) and then execute
that
SQL query.

I have done similar things with text before but not dates.

The date format on the form I want to use is DD/MM/YYYY.
I believe that SQL queries in MS Access are formatted in MM/DD/YYYY.

I was wondering if you know if I will experience any trouble with the date
format, and if you have any tips for this (or the process as a whole).

An example query at the moment using just hard dates, is as follows.
lic_grant (is licence granted date)
lic_sur (is licence surrendered date)


SELECT tblCustomer.company, tblCustomer.lic_sur, tblCustomer.lic_grant,
FROM tblCustomer
WHERE (((tblCustomer.lic_sur) Is Null) AND
((tblCustomer.lic_grant)=#6/30/2005#)) OR
(((tblCustomer.lic_sur)=#7/1/2004#) AND
((tblCustomer.lic_grant)=#6/30/2005#))
ORDER BY tblCustomer.lic_grant;



You're right, date literals must be in an unambiguous format
(e.g. # yyyy-mm-dd#) or in USA format #m/d/yyyy#

The trick is to convert your date value variable to a
literal in the SQL string. Because of differing Windows
regional settings, you need to use the Format function to do
it:

tblCustomer.lic_sur = Format(dtSur, "\#m\/d\/yyyy\#")

The reason for the \ before the / is that an unescaped /
will be replaced by the regional setting for the date
separator character, which might not be acceptable in USA
format.

--
Marsh
MVP [MS Access]



  #4  
Old May 19th, 2005, 04:18 PM
Marshall Barton
external usenet poster
 
Posts: n/a
Default

Of course it is! That's why I used "unambiguous format"
and "USA format" in two separate phrases.

Regardless, whatever you, I, or anyone else thinks is the
"natural" way to express a date, the only thing that counts
is how Windows/Access interprets it. Fortunately, or
unfortunately, depending on your point of view at the time,
Access tries very hard (sometimes I think too hard) to make
sense of all the potential ways of expressing a date so,
since there are almost an unlimited number of ways of
interpreting a string of characters as a date, some rules
must be applied. Even you and I would likely agree that 12
May 2005 is an unambiguous date, but to non-English versions
of Windows/Access, that would be incomprehensible.
--
Marsh
MVP [MS Access]



JohnFol wrote:
Marshal, just remember #m/d/yyyy# is ambiguous for all non-us countries as
it's not clear if 01/02/2005 is Feb or March.


Dylan Moran wrote:
I want to create a from which takes the user input date from a text box,
assigns it to a variable (using VBA) and then uses that variable to from
part
of my WHERE clause in an SQL query (not QBE Grid query) and then execute
that
SQL query.

I have done similar things with text before but not dates.

The date format on the form I want to use is DD/MM/YYYY.
I believe that SQL queries in MS Access are formatted in MM/DD/YYYY.

I was wondering if you know if I will experience any trouble with the date
format, and if you have any tips for this (or the process as a whole).

An example query at the moment using just hard dates, is as follows.
lic_grant (is licence granted date)
lic_sur (is licence surrendered date)


SELECT tblCustomer.company, tblCustomer.lic_sur, tblCustomer.lic_grant,
FROM tblCustomer
WHERE (((tblCustomer.lic_sur) Is Null) AND
((tblCustomer.lic_grant)=#6/30/2005#)) OR
(((tblCustomer.lic_sur)=#7/1/2004#) AND
((tblCustomer.lic_grant)=#6/30/2005#))
ORDER BY tblCustomer.lic_grant;



"Marshall Barton" wrote
You're right, date literals must be in an unambiguous format
(e.g. # yyyy-mm-dd#) or in USA format #m/d/yyyy#

The trick is to convert your date value variable to a
literal in the SQL string. Because of differing Windows
regional settings, you need to use the Format function to do
it:

tblCustomer.lic_sur = Format(dtSur, "\#m\/d\/yyyy\#")

The reason for the \ before the / is that an unescaped /
will be replaced by the regional setting for the date
separator character, which might not be acceptable in USA
format.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
format the "date" button for a header sc11 General Discussion 1 May 12th, 2005 03:48 AM
Conditonal Format with a date format Kevin General Discussion 2 April 27th, 2005 10:20 PM
Date format incorrect mail merge Emily Mailmerge 1 September 9th, 2004 10:22 AM
Date Format Date Formating Problem Database Design 3 August 16th, 2004 06:41 PM
If statement Doug Worksheet Functions 9 June 28th, 2004 06:13 AM


All times are GMT +1. The time now is 05:45 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright 2004-2024 OfficeFrustration.
The comments are property of their posters.