Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Date format

Posted on 2003-11-25
9
Medium Priority
?
16,820 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 128 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 124 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 124 total points
ID: 9824498


set dateformat dmy

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

Hilaire
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

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

:)
0
 
LVL 1

Assisted Solution

by:cerdal
cerdal earned 124 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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