Solved

SQL Date format

Posted on 2003-11-25
9
16,753 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
9 Comments
 
LVL 8

Accepted Solution

by:
dishanf 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now