Home All Groups Group Topic Archive Search About
Author
12 Oct 2007 8:12 PM
Bernard
What I am trying to do is the following:

I have a web text box the will have a date format of "11/04/2007" but I want
to query the database date field (mod_date) which format is "20071104".  How
will you write the convert statement.  Example below:

Select case_number, group_number, open_date, mod_date, case_one_line
from cases
where status = 1 and mod_date = '20071009' and group_number = '15'

Thanks in advance
--
Bernard

Author
12 Oct 2007 9:42 PM
Erland Sommarskog
Bernard (Bern***@discussions.microsoft.com) writes:
> What I am trying to do is the following:
>
> I have a web text box the will have a date format of "11/04/2007" but I
> want to query the database date field (mod_date) which format is
> "20071104".  How will you write the convert statement.  Example below:
>
> Select case_number, group_number, open_date, mod_date, case_one_line
> from cases
> where status = 1 and mod_date = '20071009' and group_number = '15'

Is mod_date char(8) or is it datetime? In the latter case, it has no
format at all, but it is a binary value.

In any case you should use a parameterised query, which will look a little
different depending on your API. If you are using SqlClient it would be:

  where status = @status and mod_date = @moddate and group_number = @num

(I assuming here that all three are parameters. If the 1 for status is a
constant, it should not be a parameter.)

How you pass the parameters depends on your API. There are several gains
with parameterised statements: protection against SQL injection,
more efficient use of the query cache in SQL Server, and in this
context: easier handling of date values. You pass the parameter as a
datetime value, and all the API interprets the string according to
the regional settings of the client.

In case mod_date is a char(8), I would prefer to pass the parameter as
char(8) and simply format it as YYYYMMDD on the client-side.



--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button