开发者

What is the best practice to query dates in SQL when parameterised queries are not possible

开发者 https://www.devze.com 2023-04-07 12:36 出处:网络
My brief is to implement a interface which has a method that looks something like \'GetValuesSqlStatement\' below:

My brief is to implement a interface which has a method that looks something like 'GetValuesSqlStatement' below:

public string SqlPattern { get { ... } }
//this varies; eg. "SELECT name FROM someTable WHERE startDate < {0}"

public string DatabaseType { get { ... } }
//this varies; eg. "SqlServer"

public string GetValuesSqlStatement(List<object> paramValues)
{
    //...desired logic here, using DatabaseType, SqlPattern and paramValues
}

Now, because this must produce an executable SQL statement, I can't use parameters in the execution of the query. And the interface I must implement is non-negotiable. What is the best way to proceed to make sure the dates in the result are interpreted by the database q开发者_运维百科uery engine correctly? Assuming that the paramValues contain .NET DateTime objects, how should these be formatted to string before plugging into the SQL pattern string? What is the most common universal date format across must databases? (eg. something like 'dd-mmm-yyyy').

NB: I only really need to worry about Sql Server from 2005 onwards and Oracle 10g onwards. So the SQL must be valid T SQL and PL SQL and mean the same thing in both flavours.


I think the only unambiguous date format for SQL Server is YYYYMMDD:

  • Bad habits to kick : mis-handling date / range queries

  • Bad Habits to Kick : Using shorthand with date/time operations

Oracle uses a DATE 'YYYY-MM-DD' notation:

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements003.htm#BABGIGCJ

While there may be a notation which works for both in some scenarios, I doubt there is one which works for both with all possible regional server settings.

Like you said, YYYY-MON-DD might be useful - it's Oracle's default.


If you use date format 'yyyy-mm-dd' with any DB you should be fine. This is as per ISO 8601 ( http://www.iso.org/iso/date_and_time_format )


I'm providing my own answer to this, even though it's veering from the scope of the question because it might be a useful suggestion for others with a similar question.

I just realised I can simply expect each instance (or each customer in a different part of the world) to optionally specify a format string in the latter part of the parameter place-holder. Eg. implementation:

public string SqlPattern { get {
    return "SELECT name FROM someTable WHERE startDate < {0:yyyy-mm-dd}";
} }  

And then my component doesn't need to worry about how to format the date at all. By default I can use 'yyyymmdd' or even better just use the server's culture to pick a default. Otherwise use the custom-specified pattern. This would be a generic approach applicable to other types that need to be formatted out to a string for SQL too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号