Link to home
Start Free TrialLog in
Avatar of Stevius
Stevius

asked on

Correct ASP Syntax for Date()-365


I want to show the order results from the last year. Therefore I used this:

sql = sql & " AND ((tblOrder.dtmInput)>'" & Date()-365"'))"

The problem is the Date()-365. Does this work in sql-asp?
Thanks to inform me when you know the exact syntax?

Regards,
Stevius
Avatar of xabi
xabi

Try this:

sql = sql & " AND ((tblOrder.dtmInput)>'" & sysdate-365"'))"

xabi
or try this other

sql = sql & " AND ((tblOrder.dtmInput)>'" & getDate()-365"'))"

xabi
Avatar of Stevius

ASKER


When I try this I get expect end of statement error.
Avatar of fritz_the_blank
Use the DateAdd() method:

DateAdd("d",-365,Date())

FtB
sql = sql & " AND ((tblOrder.dtmInput)>'" &  DateAdd("d",-365,Date() &  "'))"

FtB
Avatar of Stevius

ASKER

Thanks for the help: The error now is Expected ')'
Therefore I have copied the complete sql statement.
Everything works fine when I exclude the date rule.

sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & " AND ((tblOrder.dtmInput)>'" & DateAdd("d",-365,Date() & "'))"
sql = sql & " ORDER BY tblOrder.IDOrder;"
I would use Datediff function

sql = sql & " datediff(yy,tblOrder.dtmInput,getdate()) = 1"
sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & " AND tblOrder.dtmInput > (SysDate-30)"
sql = sql & " ORDER BY tblOrder.IDOrder;"
What happens if you do this:

response.write sql
response.end


so that we can see the output.


FtB
sql = sql & " AND tblOrder.dtmInput > (SysDate-365)"   ;)
sql = sql & " AND ((tblOrder.dtmInput)>'" & DateAdd("d",-365,Date()) & "')"

FtB
Inorder to use the sql date format, use..

sql = sql & " AND ((tblOrder.dtmInput)> (SELECT DATEADD(day, -365, (select getDate()))))"

Cheers!!
The issue was that you needed to close the parentheses for the DateAdd() method but it was included inside the quotation marks.

FtB
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think jitganguly's suggestion is the most optimised one..

Cheers!!
Avatar of Stevius

ASKER

Thanks You All !!! I used the solution of Fritz. Easy to copy and it works !!! Yeah !
I didn't had to deal with the parentheses and all the other signs that confuses me . TC, Stevius
Glad to have helped,

FtB
Avatar of Stevius

ASKER

And the db is not excessive nor immense
Thx Ftb
I still stand by my statement ... ;0)

>>I think jitganguly's suggestion is the most optimised one..<<

Cheers!!
If the DBMS supports the DateDiff() method, then that is a good way to do it (SQL Server and Access support this I believe). The reason that I went with the DateAdd is so that it could happen outside of the SQL, keeping it compatible with a wider range of DBMS's.

FtB

Avatar of Stevius

ASKER

:) Anyway I'll keep all solutions.
>>The reason that I went with the DateAdd is so that it could happen outside of the SQL, keeping it compatible with a wider range of DBMS's

1.More overheads with this solution.
2. You don't write application for different types of DBMS's but cross browser

Anyway, No offense Patrick.
Jit--

Not to be contrary, but I think that you do have to keep in mind the DBMS when writing your SQL. For example, you can include a number of different functions in a SQL statement created for Access that will fail with MySQL. Date() works fine in the former but fails in the latter.

So what I am saying here is that if you keep your functions so that they are executed in the VBScript rather than in the SQL, then you will have a wider compatibility with different DBMS's. However, if you know what DBMS you are going to use and are familiar with what methods are supported, then you may have a performace gain for using the methods within the SQL. Does that make sense?

FtB
Gee.. did i start off a war here.. LOL

Anyway, Fritz has a valid reason i guess.. but how ever.. i still believe that if application portability is not an issue, then performance should get top prority. Anyway.. In most cases, how ever well written the code is, we would have to do some re coding when porting the app. Also, what if the application portability is to happen the other way round ie, SQL/Access stays and the app is to be ported to PHP/.NET/JSP??

Anyway.. No application is 100% portable.. so i try and code for performance most of the time.. unless there is a clear need for portability and is identified during the project scoping.

Cheers!!
Well--you are absolutely correct about that. If portablility is not an issue, and the DBMS supports the function, then it is best to write for performance.

I still don't know what DBMS is being used, so I tried to code in the most generic way. If truth be told, however, I am guessing that most likely the answer is Access or SQL Server, in which case DateDiff() should be supported in the native SQL.

FtB

Agree to that.. ;o)

Cheers!!
Avatar of Stevius

ASKER

I had to leave urgently yesterday.
If anyone still interested the db used is Access and portability is not important for this one.  I agree to go for performance and am trying to make Datediff work.
Regards for all the practical info.
Stevius
Avatar of Stevius

ASKER

Apparently today it doesn't seem to work. Anybody can help?

***Error Type:***
Microsoft JET Database Engine (0x80040E07)
Data type mismatch in criteria expression.

***CODE USED***
sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & " AND ((tblOrder.dtmInput)>'" & DateAdd("d",-365,Date()) & "'))"
sql = sql & " ORDER BY tblOrder.IDOrder;"

Thanks,
Stevius
Do a response.write on the sql string and port your code here...

Cheers!!
Give this a shot--it uses the DateDiff() method discussed above:

sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & " datediff(yy,tblOrder.dtmInput,getdate()) = 1"
sql = sql & " ORDER BY tblOrder.IDOrder;"


FtB
Patrick is missing an and before datediff

sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & "  and datediff(yy,tblOrder.dtmInput,getdate()) = 1"
sql = sql & " ORDER BY tblOrder.IDOrder;"


Do I get points ? :-)
Good catch!

FtB
However, I copied it from your post above!

FtB
However I didn't put the whole SQL above on my first post  :-), so no and business
Avatar of Stevius

ASKER

After Sale Service :)

This is the sql string returned

SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode , tblBook.blnCheckIn, tblOrder.dtmInput FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder) ON tblEmployee.IDEmployee = tblOrder.IDEmployee WHERE (((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='GGB105') and datediff(yy,tblOrder.dtmInput,getdate()) = 1 ORDER BY tblOrder.IDOrder;

When I execute the sql I get an error message.

Error Type:
Microsoft JET Database Engine (0x80040E14)
Syntax error (missing operator) in query expression '(((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='GGB105') and datediff(yy,tblOrder.dtmInput,getdate()) = 1 ORDER BY tblOrder.IDOrder;'.
/intern/sb/bib_myLecture.asp, line 153

Line 153 is : rs4.open sql, cn

Thanks!
I think you have an extra "("

Try..

sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE ((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & "  and datediff(yy,tblOrder.dtmInput,getdate()) = 1"
sql = sql & " ORDER BY tblOrder.IDOrder;"

Cheers!!
Avatar of Stevius

ASKER


HAd to do something else then asp.
strange but true: Undefined function 'getdate' in expression.
Anyone knows what to do?
Thanks ;)
Try..

Try..

sql = "SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode"
sql = sql & " , tblBook.blnCheckIn, tblOrder.dtmInput"
sql = sql & " FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder)"
sql = sql & " ON tblEmployee.IDEmployee = tblOrder.IDEmployee"
sql = sql & " WHERE ((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='" & userid(1) & "')"
sql = sql & "  and datediff(yy,tblOrder.dtmInput,(select getdate())) = 1"
sql = sql & " ORDER BY tblOrder.IDOrder;"

Cheers!!
Avatar of Stevius

ASKER

! Thanks ap_sajith !

I get the following : Syntax error. in query expression '((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='GGB105') and datediff(yy,tblOrder.dtmInput,(select getdate())) = 1'.

When I execute the sql I get this :
SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode , tblBook.blnCheckIn, tblOrder.dtmInput FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder) ON tblEmployee.IDEmployee = tblOrder.IDEmployee WHERE ((tblBook.blnCheckIn)=No) AND ((tblEmployee.usercode)='GGB105') and datediff(yy,tblOrder.dtmInput,(select getdate())) = 1 ORDER BY tblOrder.IDOrder;

Hope this helps ...
Avatar of Stevius

ASKER

When I try Date() I get the message that no value is given for one or more parameters
See if this query returns any error...

SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode , tblBook.blnCheckIn, tblOrder.dtmInput FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder) ON tblEmployee.IDEmployee = tblOrder.IDEmployee WHERE datediff(yy,tblOrder.dtmInput,(select getdate())) = 1 ORDER BY tblOrder.IDOrder;


Cheers!!
Avatar of Stevius

ASKER

I get a syntax error when executed in access sql query.

It works when I use
SELECT tblOrder.IDOrder, tblBook.IDBook, tblBook.strTitle, tblEmployee.strLastName, tblEmployee.usercode , tblBook.blnCheckIn, tblOrder.dtmInput FROM tblEmployee INNER JOIN (tblOrder LEFT JOIN tblBook ON tblOrder.IDOrder = tblBook.IDOrder) ON tblEmployee.IDEmployee = tblOrder.IDEmployee WHERE datediff(yy,tblOrder.dtmInput,(date())) = 1 ORDER BY tblOrder.IDOrder;
>The only problem is that he asks for a parameter "yy"

I tried to response.write getDate > Type mismatch: 'getDate'
response.write date() returns the current date.

When I remove the sql line with datediff the query runs.

getDate is a SQL function.. not an asp function... it is equivalent to the now function is vbscript..

BTW.. is your db access or SQL?
Cheers!!
Avatar of Stevius

ASKER

Hey ap_sajith,

Finally a solution.
sql = sql & " AND ((tblOrder.dtmInput)>" & "#" & DateAdd("d",-365,Date()) & "#" & ")". Apparently with dates we need the #-sign.

Thanks !
Avatar of Stevius

ASKER

So can only be used within sql
DB still is Access  -  I'm thinking about transfer to SQL.


For access db.. yes... you need to wrap the dates in "#".. how ever its not needed in SQL server.

Cheers!!
you might have modify your queries a bit when u migrate the app to sql.... BTW.. Date Issues are the major head aches for Asp developers ;o)


Cheers!!
Avatar of Stevius

ASKER

Now I know where to find already one problem. Remove the #'s.
Ai and I still have some queries with dates to go  ...
Your tip in executing the statement in access really helped a lot.
SY, Stevius