Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to use dates in WHERE in SQL Server
Message
De
10/06/2008 14:01:37
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Divers
Thread ID:
01322704
Message ID:
01322868
Vues:
14
>Good evening,
>
>I have run into a little issue with retrieving data from SQL Server using WHERE containing DATE. Obviously the SQL Server has the date in the DATETIME format. I want to create a string that will have WHERE to retrieve records within a date range. Here is a simple version of what I tried:
>
>
>cSqlCommand = "select * from mytable where myDateTimeField >= ?06/01/2008"
>
>
>But I don't get the records that I am supposed to get. That is, I get records where DATE portion of them is before 6/1/2008.
>
>What am I missing?
>
>Thank you in advance for any help.

Dmitry,
when you want to use Parameter you have to put Variable or Field name as parameter:
lcDate = DATE(2008,6,1)
cSqlCommand = "select * from mytable where myDateTimeField >= ?m.lcDate"

** or
CREATE CURSOR crsTest (Fld1 D)
INSERT INTO crsTest VALUES (DATE(2008,6,1))
cSqlCommand = "select * from mytable where myDateTimeField >= ?crsTest.Fld1"
When you want to send Date as value built-in the string, then better use so called ISO standard (yyyymmdd) and pass it as string. SQL Server will parse it w/o any troubles. Sending DateTime that way is setting independent.
lcDate = DATE(2008,6,1)
cSqlCommand = "select * from mytable where myDateTimeField >= '"+DTOS(lcDate)+"'"
*** result should look like this:
** select * from mytable where myDateTimeField >= '20080601'
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform