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
240 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Transaction logs 8 28
Update in Sql 7 30
Designing and Implementing a Data Warehouse 3 16
Find data in a column which is not in  a date format 29 16
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

16 Experts available now in Live!

Get 1:1 Help Now