Solved

Is it possible to query database on date field using uk date format or do I use convert?

Posted on 2008-10-24
7
242 Views
Last Modified: 2012-05-05
I would like to be able to write a query using uk date format

select * from xxx where datefield >= '01/04/2008'

also is there a way to get dates returned from query displayed in uk format or
do I have to use convert in select statement?
0
Comment
Question by:deNZity
  • 2
  • 2
7 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 22801497
That query looks fine.  The default display format of the query likely depends on your installation settings and/or query tool configuation. If you want to request a specific format, by all means convert it to a string of the dexired form.  
0
 

Author Comment

by:deNZity
ID: 22801601
Thanks for the reply,
the following query returns 8 rows where it should only return the last 2 rows.

select * from XXXX
where TransactionDate >= '01/08/2008'


2008-02-08 00:00:00.000
2008-02-18 00:00:00.000
2008-03-28 00:00:00.000
2008-04-10 00:00:00.000
2008-05-02 00:00:00.000
2008-07-16 00:00:00.000
2008-08-01 00:00:00.000
2008-08-29 00:00:00.000
0
 

Author Comment

by:deNZity
ID: 22801608
My regional settings are set to english NZ which is the same as English UK
0
 
LVL 22

Accepted Solution

by:
dportas earned 250 total points
ID: 22802799
Best practice is to use ISO format for date literals. They will work correctly under any regional settings. Use either:

SELECT * FROM xxx WHERE datefield >= '20080801';

or

SELECT * FROM xxx WHERE datefield >= '2008-08-01T00:00:00';

or

SELECT * FROM xxx WHERE datefield >= '2008-08-01T00:00:00.000';

Hard-coding dates in a query is rather unusual though. If you use datetime variables then you won't need to worry about formatting because datetime values don't have any format. It's only in the unusual case where you want to hard-code a date that you should have to resort to the ISO format.


0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 22804673
Under Server Properties, check your Default Language property.  That governs whether dates are interpreted as mdy (what you are getting) or dmy (what you desire).   US English is dmy.  You can present your dates in the mdy form or you can change the date format using "SET DATEFORMAT dmy"
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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