Solved

SQL Date format

Posted on 2003-11-25
9
16,776 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article describes some very basic things about SQL Server filegroups.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now