473,387 Members | 1,899 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,387 software developers and data experts.

Discussion: SQL Date Literals and Regional Settings

NeoPa
32,556 Expert Mod 16PB
** NB ** This thread was split away from a technical question (Help on trouble handling date.).

Dates in SQL don't use Regional Settings.

This is why we get this question (or similar) so many times here. It doesn't help that many 'solutions' given out by experts seem to ignore this. The main reason for this, of course, is that the SQL standard format (which, as I said, is independent of Region) is the same as the Regional format for the USA (IE. m/d/yyyy). This means that a large part of the world never sees the problems involved with writing dates into SQL (and filters also contain SQL instructions of course) just as they come. Access uses Regional Settings to display dates, so date literals need to be formatted explicitly to work reliably in SQL (Even outside of the USA dates are often interpreted correctly even though in the wrong format because the SQL engine recognises dates such as 22/11/2011 as really being 11/22/2011 because there are only twelve months in a year).

For more on this see Literal DateTimes and Their Delimiters (#).

PS. To format a date correctly you can always use the following :
Expand|Select|Wrap|Line Numbers
  1. Format([DateVal], "\#m/d/yyyy\#")
It is not always necessary to include the hashes (#) in there though, as these can easily be appended as part of the rest of the SQL string.
Dec 1 '11 #1
18 5066
TheSmileyCoder
2,322 Expert Mod 2GB
Sorry NeoPa.
Im afraid its a bit more complicated then that. You see, if I with my regional settings, and my settings for what charecter I want shown between the day, month and year is not compatible with the VBA/SQL engine trouble is just around the corner.

For example if I were to use the immediate pane:
Expand|Select|Wrap|Line Numbers
  1. ? Format("2011-11-30","mm/dd/yyyy") 
VBA returns: "11-30-2011"
Note that even though I specified slashes in the format command, VBA returns with dashes (as specified by my local computer settings).
But Mihail typing the same command is getting from VBA: "11.30.2011".

Now the difference between these two results, and especially how VBA treats the two becomes more obvious with a little test:
Expand|Select|Wrap|Line Numbers
  1. ? Isdate("11-30-2011") returns TRUE
  2. ? Isdate("11.30.2011") returns FALSE
So VBA is simply not able to understand the value Mihail is getting as a date.

In his case, I presume the answer lies in using a custom function such as:
Expand|Select|Wrap|Line Numbers
  1. Public Function formatUSDate(dtInput As Date) As String
  2.     Dim strDate As String
  3.     formatUSDate = "#" & Month(dtInput) & "/" & Day(dtInput) & "/" & Year(dtInput) & "#"
  4. End Function
Dec 1 '11 #2
NeoPa
32,556 Expert Mod 16PB
Wow Smiley. That was a shock. All this time I thought I had that all well understood, but my experience has nearly all been about European countries which use slashes and the USA. Everywhere with slashes (/) even if the order changes. So even my 'portable' code is not 100% portable! That's a nasty blow I have to admit.

I will look at this as a matter of priority and see what I can come up with (and if I find anything you two can be my accomplices and test it out for me. That would be very helpful).
Dec 1 '11 #3
NeoPa
32,556 Expert Mod 16PB
This is horrible. I think I may have found a viable solution, but I corrected someone recently for using it (I just explained it wasn't necessary really - but I was quite wrong it seems).

Anyway, try the following format for me if you would guys (The Immediate Pane should do, but if you have a SQL string to try it out within too that would be even better.) :
Expand|Select|Wrap|Line Numbers
  1. Format(Date, "\#m\/d\/yyyy\#")
Essentially, this escapes the slashes (/) so they are not recognised as such when it comes to the conversion for local settings, but leaves them as slashes in the same place in the resultant string.

PS. My grateful thanks Smiley, for pointing me so clearly in the right direction. It's bad enough being wrong, but leading others astray is intollerable and you've saved me from continuing with that.
Dec 1 '11 #4
TheSmileyCoder
2,322 Expert Mod 2GB
@NeoPa
That worked very nicely for me. Results shown below:
Expand|Select|Wrap|Line Numbers
  1. ? Format(Date, "\#m\/d\/yyyy\#")
  2. #12/1/2011#
  3. ? Format("30-11-2011", "\#m\/d\/yyyy\#")
  4. #11/30/2011#
  5. ? Format("11-30-2011", "\#m\/d\/yyyy\#")
  6. #11/30/2011#
Running 100.000 iterations of your method versus mine, on dates formatted both as US and European reveals your method is 3 times faster then mine. That said 100.000 iterations still only took 470 ms with your method, so still worth looking it, should you be formatting 100k+ records.


I also find your method more gracefull then mine, allthough I will still encapsulate it into my FormatUsDate function, since I find it easier to look at that way.
Expand|Select|Wrap|Line Numbers
  1. Public Function formatUSDate(dtInput As Date) As String
  2.     formatUSDate = Format(dtInput, "\#m\/d\/yyyy\#")
  3. End Function
I also did not know about this issue, until investigating this thread in more detail, but after all, thats part of the reason I visit Bytes, to learn new stuff myself. While I do enjoy the "Feel good" of helping others I still firmly believe that I gain something myself as a programmer with each question I answer or when I look at what you and other experts answered, which is also the case for this thread. The thought of escaping the slashes in the format string is not something I would have thought of myself.
Dec 1 '11 #5
NeoPa
32,556 Expert Mod 16PB
Thank you Smiley. That's very nice to hear. I think I may have to do some more work on that article now after all this. Never mind. It enhances the quality and that's all good.

NB. I would consider renaming your function to formatSQLDate() as there are already far too many people around who don't appreciate even that SQL has a set standard for formatting date literals. It is for this reason, rather than anything to do with USA, that it is formatted that way. It's just a cosmetic detail really, but worth considering I believe.
Dec 2 '11 #6
NeoPa
32,556 Expert Mod 16PB
FYI: I've updated the article linked in the first post (Literal DateTimes and Their Delimiters (#)) to reflect this potential problem so it now includes instructions to handle it.
Dec 2 '11 #7
patjones
931 Expert 512MB
I'm only kind of sort of following the discussion. I did the same thing and got the same result as you guys:

Expand|Select|Wrap|Line Numbers
  1. ? Format(Date, "\#m\/d\/yyyy\#")
  2. #12/2/2011#

But I also did this, and got the same result:

Expand|Select|Wrap|Line Numbers
  1. ? Format(Date, "\#m/d/yyyy\#")
  2. #12/2/2011#

It makes sense to me that the backslashes in this case appear to escape the "#". This leads to ask though what the backslashes in the first version after the "m" and "d" respectively do.

In all of my projects over the years, I have consistently used mm/dd/yy format for storage in Access tables, display on forms and reports, and the input mask in data entry fields. It's just my preference and not a single user of any of my applications has questioned it. Now, if I pass one of my dates into Format( ) as above I get:

Expand|Select|Wrap|Line Numbers
  1. ? Format("05/15/13", "\#m\/d\/yyyy\#")
  2. #5/15/2013#

This is great. BUT, suppose now that I try to pass the following into an EXEC that calls a SQL Server stored procedure, which INSERTs the date into a table on the server:

Expand|Select|Wrap|Line Numbers
  1. Format(Me.fldExpirationDate, "\#m\/d\/yyyy\#")

Here fldExpirationDateis just a text box on my form that is date formatted mm/dd/yy. This gives rise to an ODBC error. However, if I pass it in as a string (which has been my standard practice when EXECing from VBA), then it goes in fine:

Expand|Select|Wrap|Line Numbers
  1. "'" & Me.fldExpirationDate & "'"

On the SQL Server side, this results in a table entry that looks like '2013-05-15' or whatever (it's a DATETIME column). I don't particularly like treating dates as strings; but it's the only way I've been able to make it work so far. I don't know if I'm making any sense here. At this point I'm blabbing on about how confounding dates are and I'll just stop.

Pat
Dec 2 '11 #8
NeoPa
32,556 Expert Mod 16PB
patjones:
It makes sense to me that the backslashes in this case appear to escape the "#". This leads to ask though what the backslashes in the first version after the "m" and "d" respectively do.
Pat, if you look at the linked article again now, you'll see a recently added section that explains that for you. I doubt it will impact your usual useage as your date format separator characters are already slashes (/). In fact your date formats are similar in all ways to the SQL format, so no problems will ever be noticed in any of your databases unless they are used abroad, and particularly in areas that don't use the slash character for dates.

patjones:
In all of my projects over the years, I have consistently used mm/dd/yy format for storage in Access tables
This is a flawed statement, as it presupposes that dates can be stored in the form of their format. This is not true at all. Dates are stored as Double Precision floating point numbers representing the number of days since 30 December 1899. This is lucky, because those of us that remember Y2K clearly (as opposed to most of the media who completely misunderstood why no planes fell from the sky), know how many problems result from 2 digit years when changing centuries.

patjones:
This is great. BUT, suppose now that I try to pass the following into an EXEC that calls a SQL Server stored procedure, which INSERTs the date into a table on the server:
SQL Server is exceptional in that it seems to pay no heed whatsoever to the ANSI-92 standards in this regard. In fact, I struggled exactly as you have done. I hated the fact that I could find no way of specifying a date/time literal within T-SQL and always had to resort to a function call that took a string parameter and converted it at run time. I'd be happy to hear that I'm mistaken on this point, but I was never able to find anything to explain why it's done the way it is in SQL Server. Thankfully, Access's Jet SQL does seem to conform to the standards in this respect.

Hopefully, these points will make your understanding of dates, and working with them, more solid in future :-)
Dec 2 '11 #9
patjones
931 Expert 512MB
NeoPa:
In fact your date formats are similar in all ways to the SQL format
That's what I don't understand. How is '2013-05-15' similar to '05/15/13'? They seem totally different to me.

NeoPa:
This is a flawed statement, as it presupposes that dates can be stored in the form of their format.
Yes, I did know that dates are stored as floating point numbers. I suppose I should have said that I always format dates to appear in mm/dd/yy form. I must try to remember not to get sloppy with my semantics.
Dec 2 '11 #10
NeoPa
32,556 Expert Mod 16PB
patjones:
That's what I don't understand. How is '2013-05-15' similar to '05/15/13'? They seem totally different to me.
Everything I saw in your post indicated you were using mm/dd/yyyy as your format. I assumed that was your Regional Setting (Nothing indicated otherwise). I'll read it through again bearing this new information in mind and see if I can understand what it is you're stuck on.
Dec 2 '11 #11
patjones
931 Expert 512MB
My regional setting is set to that, but what I was referring to is that I always set the format of my text boxes, etc. to mm/dd/yy in order to force two digit display regardless of the day or month (e.g. 05/08/11 as opposed to 5/8/11 or 5/8/2011). (I understand that using a two-digit year may not be best practice, but I don't think it's a huge deal. That may be a topic for another thread).
Dec 2 '11 #12
NeoPa
32,556 Expert Mod 16PB
patjones:
NeoPa:
In fact your date formats are similar in all ways to the SQL format
That's what I don't understand. How is '2013-05-15' similar to '05/15/13'? They seem totally different to me.
Now I'm really confused. So where does the '2013-05-15' format come into the question? I assumed (from the associated quote) that the former was your regional format and the latter was the SQL format (ignoring # of digits in years which isn't a specific part of the standard anyway). Now you say your regional format is the same as the SQL standard I'm wondering what the question is.

patjones:
My regional setting is set to that
Actually, your statement (included here) may not have meant what I read it to mean at all. The 'that' in your sentence could refer equally well to both the SQL format or the yyyy-mm-dd format. Why don't you explain and we can take it from there.
Dec 2 '11 #13
patjones
931 Expert 512MB
In post #9, you said that my "date formats are similar in all ways to the SQL format". Well, dates in my SQL Server tables always appear like yyyy-mm-dd. This is different from the regional format on my desktop machine, which is m/d/yyyy. It is also different from the mm/dd/yy format that I use for all of my Access work. In light of those observations, I was looking for clarification as to the above quoted statement that you made. It is not a crucial issue, as I have not yet encountered problems with SQL Server's date format; it is merely a curiosity on my part. Thanks.
Dec 6 '11 #14
NeoPa
32,556 Expert Mod 16PB
That makes more sense Pat. I understand the confusion now. I should possibly have been clearer - The SQL format I was referring to has nothing to do with SQL Server or T-SQL at all (unfortunately) as it refers to the SQL-92 standard, which SQL Server doesn't seem to comply with, for reasons I've never discovered.

See post #9 for some clarification of these points.
Dec 6 '11 #15
I don't know if it is a good Solution but due to dealing with a ton of related troubles i created this function...and yes i know it is slow
Expand|Select|Wrap|Line Numbers
  1. Public Function SQLDate(InputDate, Optional DebugState As Boolean) As String
  2. On Error Resume Next
  3. Dim Sday, SMonth, Syear, OutputString As String
  4. If Not IsDate(InputDate) Then Exit Function
  5. Syear = Format(Year(DateValue(InputDate)), "0000")
  6. SMonth = Format(Month(DateValue(InputDate)), "00")
  7. Sday = Format(Day(DateValue(InputDate)), "00")
  8. OutputString = Syear & "-" & SMonth & "-" & Sday
  9. If DebugState Then
  10. MsgBox "Input Value is : " & InputDate & vbCrLf & _
  11.         "Decoded Year is : " & Syear & vbCrLf & _
  12.         "Decoded Month is : " & SMonth & vbCrLf & _
  13.         "Decoded Day is : " & Sday & vbCrLf & _
  14.         " SQL will be feeded " & OutputString
  15. End If
  16. SQLDate = OutputString
  17. End Function
Dec 9 '11 #16
NeoPa
32,556 Expert Mod 16PB
I'm afraid that code doesn't really handle the situation too well. I suggest you read the linked article (Literal DateTimes and Their Delimiters (#)) for a better understanding of the various problems involved. The OutputString you should be expecting for Christmas Day is "#12/25/2011#" (Yours appears as "2011-12-25").
Dec 9 '11 #17
if i remember correctly sql likes the "yyyy-mm-dd" format.....but this is something a read a long time ago and maybe i am wrong
Dec 13 '11 #18
NeoPa
32,556 Expert Mod 16PB
This is a very late reply.

You weren't wrong. I found yet another new aspect of SQL dates and their formatting. It was a while ago now, but I suspect your comment was the trigger for me looking again.

The linked thread should now include that and I actually use that format now as standard.
May 22 '16 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David Lee AU | last post by:
We have an ASP.NET page that displays a date without a format specifier (in order to determine to default locale setting) and the date is shown as mm/dd/yyyy. We previously were able to set the...
1
by: Laurence Neville | last post by:
This is regarding a change in the Short Date format under Hebrew Regional Settings, that has caused huge problems in our ASP web application. The change appears to have been introduced sometime...
4
by: Jonathan | last post by:
Dear All, I am trying to set a webserver to use French regional settings for testing ASP pages. According to http://support.microsoft.com/kb/q306044, for IIS5, this is a matter of changing...
2
by: isaacrajan | last post by:
Hello, Is there a way in which users can be prevented from making changes to regional settings in Win XP Professional edition so that the interpretation of dates by an Access application remains...
3
by: Karunakararao | last post by:
Hi All, How can i get the date based on Regional settings . how can i get the date format using C# .NET " The date displayed in the date/time field should be formatted according to the...
12
by: magister | last post by:
Hello, I know I can set the Culture to what I want, but shouldn't the current culture be taken from the regional settings on the web server's control panel!!! Mine is set to "united kingdom"...
7
by: Fred Flintstone | last post by:
I'm writing a VB.Net windows forms application. This line of code: Personal.EffectiveDate = GridRow2.Cells("New Value").Value.ToString.Trim Fails with this error: Cast from string...
1
by: Lauren Quantrell | last post by:
I already figured out (the hard way) I need to convert all my date parameters into USA format before executing my stored procedures where dates are used as parameters. (Format(StartDate, "m/d/yyyy...
0
by: SharmaPunit | last post by:
I want to display a date in m/d/yy format. I have written the following lines of code and it is running perfectly till the date format in the regional settings of my computer is set to mm/dd/yyyy or...
3
by: Abdul Qadir | last post by:
Hi, I am using Javascript for Excel Export from classic ASP. In my excel i have many Date columns that i need to store based on the regional settings. The application also allow us to set the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.