SQL-statement using Date-function? 
Author Message
 SQL-statement using Date-function?

Dear friends

In a table a field is containing dates. From the table I want to retrieve
all records where fldDate is within the previous 7 days. I tried this:

mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

But this cause error like syntaxerror in date in query....

I have also tried this:

mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

Anybody helps me with this task and tell me what is wrong?

Regards Able



Wed, 23 Apr 2003 09:07:25 GMT  
 SQL-statement using Date-function?
Try using between in your SQL statement...

 mySQL = "SELECT * FROM Table WHERE fldDate Between #" & fromDate & "#" _
 & " And #" & toDate & "#;"

--


Quote:
> Dear friends

> In a table a field is containing dates. From the table I want to retrieve
> all records where fldDate is within the previous 7 days. I tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

> But this cause error like syntaxerror in date in query....

> I have also tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

> Anybody helps me with this task and tell me what is wrong?

> Regards Able



Wed, 23 Apr 2003 03:00:00 GMT  
 SQL-statement using Date-function?


Quote:
> Dear friends

> In a table a field is containing dates. From the table I
want to retrieve
> all records where fldDate is within the previous 7 days. I
tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date -
7 & "#"

> But this cause error like syntaxerror in date in query....

> I have also tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

> Anybody helps me with this task and tell me what is wrong?

> Regards Able

I presume the 'Date'  you mention above is not the name of
the variable as it is a reserved word.  You need to do
something like:

mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7
& "#"

dim dtLastWeek as date
dim dtNow as date

dtNow = Now

dtLastWeek = DateAdd("d", -7, dtNow)

mySQL = "SELECT * FROM Table WHERE fldDate >=  #"
Month(dtLastweek) & "/" &  Day(dtLastWeek) & "/" &
Year(dtLastweek) & "#"

--
John Blessing
http://www.LbeHelpdesk.com - home of the free helpdesk



Wed, 23 Apr 2003 03:00:00 GMT  
 SQL-statement using Date-function?


Quote:



> > Dear friends

> > In a table a field is containing dates. From the table I
> want to retrieve
> > all records where fldDate is within the previous 7 days.
I
> tried this:

> > mySQL = "SELECT * FROM Table WHERE fldDate >=  #" &
Date -
> 7 & "#"

> > But this cause error like syntaxerror in date in
query....

> > I have also tried this:

> > mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date -
7

> > Anybody helps me with this task and tell me what is
wrong?

> > Regards Able

> I presume the 'Date'  you mention above is not the name of
> the variable as it is a reserved word.  You need to do
> something like:

> mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date -
7
> & "#"

> dim dtLastWeek as date
> dim dtNow as date

> dtNow = Now

> dtLastWeek = DateAdd("d", -7, dtNow)

> mySQL = "SELECT * FROM Table WHERE fldDate >=  #"
> Month(dtLastweek) & "/" &  Day(dtLastWeek) & "/" &
> Year(dtLastweek) & "#"

> --
> John Blessing
> http://www.LbeHelpdesk.com - home of the free helpdesk

Oops...  Ignore the
Quote:
> mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date -
7
> & "#"

 just after the "You need to do something like:".  I meant
to delete it after copying from earlier in the message.
--
John Blessing
http://www.LbeHelpdesk.com - home of the free helpdesk
"


Thu, 24 Apr 2003 03:00:00 GMT  
 SQL-statement using Date-function?

Quote:

>Dear friends

>In a table a field is containing dates. From the table I want to retrieve
>all records where fldDate is within the previous 7 days. I tried this:

>mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

>But this cause error like syntaxerror in date in query....

Beats me!  I tried out something very like this and it worked just fine.  Of
course, being a pedant, I always put a ';' on the end of my SQL strings, but
I doubt that made the difference.

I am assuming that your fldDate field IS set to the Date datatype?
Could you post the entire code of your Sub/Function?



Fri, 25 Apr 2003 08:56:07 GMT  
 SQL-statement using Date-function?
are you sending parameter values using the Command Parameter object?

i think you should use

mySQL = "SELECT * FROM Table WHERE fldDate >=  ?"

and

Dim oParam as new ADODB.Parameter

oParam.Value = dDate-7
oParam.Type = adDate   or maybe adDBDate

oConnection.Parameters.Append oParam

where oConnection is your ADO connection object

Quote:

>Dear friends

>In a table a field is containing dates. From the table I want to retrieve
>all records where fldDate is within the previous 7 days. I tried this:

>mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

>But this cause error like syntaxerror in date in query....

>I have also tried this:

>mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

>Anybody helps me with this task and tell me what is wrong?

>Regards Able



Tue, 06 May 2003 03:00:00 GMT  
 SQL-statement using Date-function?
Put the date in a string variable using sDate = Date -7 then query against
the variable.

"Select * from tablename where field >= '" & sDate


Quote:
> Dear friends

> In a table a field is containing dates. From the table I want to retrieve
> all records where fldDate is within the previous 7 days. I tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

> But this cause error like syntaxerror in date in query....

> I have also tried this:

> mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

> Anybody helps me with this task and tell me what is wrong?

> Regards Able



Tue, 06 May 2003 03:00:00 GMT  
 SQL-statement using Date-function?
Dim dStart as date, MySQL as string
dStart = date - 7
mySQL = "select * from table where fldDate >= '" & dStart & "'"


Quote:
> are you sending parameter values using the Command Parameter object?

> i think you should use

> mySQL = "SELECT * FROM Table WHERE fldDate >=  ?"

> and

> Dim oParam as new ADODB.Parameter

> oParam.Value = dDate-7
> oParam.Type = adDate   or maybe adDBDate

> oConnection.Parameters.Append oParam

> where oConnection is your ADO connection object


> >Dear friends

> >In a table a field is containing dates. From the table I want to retrieve
> >all records where fldDate is within the previous 7 days. I tried this:

> >mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

> >But this cause error like syntaxerror in date in query....

> >I have also tried this:

> >mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

> >Anybody helps me with this task and tell me what is wrong?

> >Regards Able



Wed, 07 May 2003 03:00:00 GMT  
 SQL-statement using Date-function?
Depending on the database you are accessing, there could be a few problems.
You might try this
dim sDate as string
sDate = format(dateadd("d",-7,Date),"mm/dd/yyyy")  'to both subtract the
seven and format the date into a convertable string

Then use your sql statement
"Select * from tablename where field >= '" & sDate & "'"
don't forget that second delimeter


Quote:
> Put the date in a string variable using sDate = Date -7 then query against
> the variable.

> "Select * from tablename where field >= '" & sDate



> > Dear friends

> > In a table a field is containing dates. From the table I want to
retrieve
> > all records where fldDate is within the previous 7 days. I tried this:

> > mySQL = "SELECT * FROM Table WHERE fldDate >=  #" & Date - 7 & "#"

> > But this cause error like syntaxerror in date in query....

> > I have also tried this:

> > mySQL = "SELECT * FROM Table WHERE fldDate >= " & Date - 7

> > Anybody helps me with this task and tell me what is wrong?

> > Regards Able



Sat, 20 Sep 2003 04:38:27 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Comparing dates using SQL statement

2. : sql statement..group using dates

3. error writing date field to Access 2000 using SQL statement in VB 6

4. Date to SQL from VB using Insert statement

5. Using a query/SQL statement as a function

6. Unable to modify SQL statement using string functions

7. Using Functions in SQL Statement

8. Using a Function within an SQL-Statement

9. Dates in SQL update statements

10. Variable Date in a SQL statement

11. Date formatting in a SQL statement

12. SQL statement with Date Fields

 

 
Powered by phpBB® Forum Software