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
Solved

SQL Date format

Posted on 2003-11-25
9
16,787 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:
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
compare between entities in entity framework 8 94
Server configuration for MS Access - SQL Server app 8 77
Foxpro errors 4 42
Teradata converting character to integer 2 26
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

861 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