Solved

SQL Date format

Posted on 2003-11-25
9
16,790 Views
Last Modified: 2012-06-27
Hi everyone,

Platform: Windows XP Professional + SP1a, Office XP Professional + SP2, SQL Server 2000 + SP3a, Microsoft Excel 2002.

I am experiencing a bit of difficulty with some dates in a query of mine.  I'm using the Microsoft Calendar control as a date picker on the client, and it works just fine.  I'm in Australia, so the date format is spits out is in the format: dd/mm/yyyy.  Unfortunately, the sql server is reading the date as mm/dd/yyyy, which fails for values like 13/01/1979.  Thinking it was a difference in regional settings between the client and the server, I checked and found that they are both set to use English(Australia) as both the system and the user input locales.

I would really appreciate some help with this, I'm sure it's going to be a setting I have overlooked.

Thanks in advance...
0
Comment
Question by:Know-Wires
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 8

Accepted Solution

by:
Dishan Fernando earned 32 total points
ID: 9823513
SELECT CONVERT( varchar(10),DateColumn,103)
FROM TableName

Replace DateColumn & TableName with ur ones
0
 
LVL 3

Assisted Solution

by:Flynnious
Flynnious earned 31 total points
ID: 9824373
For some reason known only to MS, the SQL Server install uses its own setting for date format, completely separate from the date setting for the machine it's running on.  I believe the format used by SQL is set at install time or similar, and it's quite difficult to change (at least, I've never managed it, but I've never looked that deeply into it).  A better thing to do is write the dates to the database in ISO standard date format (the same format that SQL returns that data in when querying the database).  I appreciate this is quite difficult to arrange for the applications that use SQL, but it's the only reliable way I've found of working with dates in SQL.  ISO standard date reads YYYY-MM-DD HH:MM:SS, although you don't have to specify the time.

So today's date is 2003-11-26

HTH

Flynnious
0
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 31 total points
ID: 9824498


set dateformat dmy

insert into mytable(datefield) values('13/01/1979')

Hilaire
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 3

Expert Comment

by:Flynnious
ID: 9824509
Cool!  You learn something new every day!

:)
0
 
LVL 1

Assisted Solution

by:cerdal
cerdal earned 31 total points
ID: 9826140
In SQL Server 6.5 you use the CONVERT function :

CONVERT(DATETIME , MyDateValue , 103)

the online documentation gives the values for the 3rd parameter.

here's a doctored extract, excluding where it deals with time values:

---------------------------------8<------------------------------

1      mm/dd/yy
2      yy.mm.dd
3      dd/mm/yy
4      dd.mm.yy
5      dd-mm-yy
6      dd mon yy
7      mon dd, yy
10      mm-dd-yy
11      yy/mm/dd
12      yymmdd

Add 100 to a style value to get a four-place year that includes the century (yyyy).

---------------------------------8<------------------------------

so style 103 means dd/mm/yyyy

I use Delphi and SQL Server, and I call a function to format Delphi date values for insertion into dynamic SQL scripts:

function MSSQLDate(Dt:TDateTime):String;
begin
  try
    Result := 'CONVERT(datetime,'''+FormatDateTime('yyyy-mm-dd',Dt)+''',102)';
  except
    Result := '';
  end;
end;

You could perhaps do something similar for your string values?
0
 
LVL 1

Expert Comment

by:cerdal
ID: 9826369
sorry, rereading my post, I see that my function should have 110 instead of 102 in it. But funny thing is it's worked up to now. Perhaps SQL Server only looks at the digit positioning when reading values in.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question