tags:

views:

516

answers:

5

Let's say I have a table with a Date column. In my C# application, I want to be able to query for rows using the LIKE operator on the DateTime column. My problem is, how do I handle the regional settings?

For example, if the current regional settings are set to de-DE, the date format of windows is dd.mm.yyyy. Now the user might enter something like %2009, resulting in the following query:

select * from MyTable where to_char(MyDateColumn, 'dd.MM.yyyy') like '%2009'

But with different regional settings, the input of the user will look different of course, so to_char doesn't work anymore.

How can I work around this? Oh, and I'm looking for a database-independent solution.

Many thanks in advance!

+2  A: 

Don't convert it into textual form!!!

Use DATEPART to check this: http://msdn.microsoft.com/en-us/library/aa258265(SQL.80).aspx

eg:

select * from MyTable where DATEPART(year, MyDateColumn) = 2009
Tommi Forsström
But the datepart function does only work on MS SQL Server, right? I'm looking for a database-independent solution...
Christian Hubmann
A: 

Don't compare dates with strings. Instead, use something like the DATEPART function to compare to just the year.

John Saunders
But the datepart function does only work on MS SQL Server, right? I'm looking for a database-independent solution...
Christian Hubmann
I don't know whether DATEPART is T-SQL only. Same with the YEAR function.
John Saunders
+6  A: 

You don't want to use a LIKE operator on a DATETIME column, believe me.

select * from MyTable where year(MyDateColumn) = 2009

or

select * from MyTable where datepart(yy, MyDateColumn) = 2009
Tomalak
"I" don't want to use a LIKE operator on a DateTime column, but a customer is requesting this feature...The year and datepart functions only work on MS SQL Server, right?
Christian Hubmann
The customer doesn't want to use the LIKE operator either, I'm quite sure. What exactly are they requesting?
Tomalak
Well, if there's a DateTime box on a form, they want to be able to query with placeholders, like %2009 to get all rows for 2009, for example.
Christian Hubmann
What should happen if they enter "%12009", or %1209%"? (Don't say that this can't happen.)
Tomalak
It should happen what you would expect from LIKE ;) "%12009" -> date values with months ending with 1 in the year 2009. "%1209%" -> ah... i guess that would be 12th of september in any year. I think you get the point ;)
Christian Hubmann
My comment was less of a question and more of a means to point out that your approach itself it flawed. 1209 can be the 9th of December as well, or the December '09. Why do you think you can fight ambiguity in your SQL when you don't in your user interface?
Tomalak
My idea is to use to_char(MyDateCol, :pDateFormatOfUser) like :pUserInput. Of course I would have to convert the C# date format to a sql date format. That way I wouldn't have the problem you pointed out. This could work, but it doesn't look right. I thought there might be a better solution.
Christian Hubmann
+1 for your help ;)
Christian Hubmann
Hm... Somehow I'm not sure that I actually helped you. :-) However if you confine the input to, say "DDMMYYYY", allowing question marks in place of single digits, and let that run against "to_char(MyDateColumn, 'ddMMyyyy') LIKE :pUserInput", it might work.
Tomalak
That actually is the way we do it right now, but the problem is: what if the user is sitting in another country and uses different settings for the date format?
Christian Hubmann
Well, don't let him enter a *date* - let him enter a fixed format string that consists of eight symbols (either digits or question marks). Make clear that you expect them to enter "DDMMYYYY", and local customs in date representation become irrelevant.
Tomalak
I'm afraid that's not what the user is expecting... Let's say you show a date value in the prefered format of the user, but when querying he has to use a different format?
Christian Hubmann
Hm... Then the application logic should mediate. Make the date input format dependent on current locale, reorder the input string and feed that to the database. I'm not sure if you will be able to create a truly database-independent solution on the SQL side, date functions vary a lot between vendors.
Tomalak
Hm... that's an idea. Thanks :)
Christian Hubmann
+2  A: 

And specially, do use parametrised queries to prevent SQL Injection attacks.

Miha Markic
Yes, I'm doing that. The select I wrote was just as an example. But you have a point ;)
Christian Hubmann
Great. Believe me, making such comments is a good thing because a lot of people isn't aware of that problem.
Miha Markic
Hm... I guess you're right ;)
Christian Hubmann
A: 

Display date in dd/mm/yy format

SELECT [Sales_id] ,[sales_no] ,[sequence_id] ,[sales_Date], CONVERT(char(12), sales_Date, 3) as sales_Date1 ,[Net_Total] ,[Roundoff] FROM [Furniture].[dbo].[SalesMaster]

288 109 1 2010-08-21 00:00:00.000 21/08/10 1040.00

291 110 1 2010-08-21 00:00:00.000 21/08/10 103.00

294 111 1 2010-08-21 00:00:00.000 21/08/10 7128.00 -0.40

Please Refer http://msdn.microsoft.com/en-us/library/aa172602(SQL.80).aspx

B.Jeevanandam