[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

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
0
Stevius
Asked:
Stevius
  • 15
  • 13
  • 12
  • +2
1 Solution
 
xabiCommented:
Try this:

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

xabi
0
 
xabiCommented:
or try this other

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

xabi
0
 
SteviusAuthor Commented:

When I try this I get expect end of statement error.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
fritz_the_blankCommented:
Use the DateAdd() method:

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

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

FtB
0
 
SteviusAuthor Commented:
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;"
0
 
jitgangulyCommented:
I would use Datediff function

sql = sql & " datediff(yy,tblOrder.dtmInput,getdate()) = 1"
0
 
xabiCommented:
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;"
0
 
fritz_the_blankCommented:
What happens if you do this:

response.write sql
response.end


so that we can see the output.


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

FtB
0
 
ap_sajithCommented:
Inorder to use the sql date format, use..

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

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

FtB
0
 
fritz_the_blankCommented:
So, this should do it:

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;"
0
 
ap_sajithCommented:
I think jitganguly's suggestion is the most optimised one..

Cheers!!
0
 
SteviusAuthor Commented:
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
0
 
fritz_the_blankCommented:
Glad to have helped,

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

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

Cheers!!
0
 
fritz_the_blankCommented:
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

0
 
SteviusAuthor Commented:
:) Anyway I'll keep all solutions.
0
 
jitgangulyCommented:
>>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.
0
 
fritz_the_blankCommented:
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
0
 
ap_sajithCommented:
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!!
0
 
fritz_the_blankCommented:
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

0
 
ap_sajithCommented:
Agree to that.. ;o)

Cheers!!
0
 
SteviusAuthor Commented:
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
0
 
SteviusAuthor Commented:
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
0
 
ap_sajithCommented:
Do a response.write on the sql string and port your code here...

Cheers!!
0
 
fritz_the_blankCommented:
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
0
 
jitgangulyCommented:
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 ? :-)
0
 
fritz_the_blankCommented:
Good catch!

FtB
0
 
fritz_the_blankCommented:
However, I copied it from your post above!

FtB
0
 
jitgangulyCommented:
However I didn't put the whole SQL above on my first post  :-), so no and business
0
 
SteviusAuthor Commented:
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!
0
 
ap_sajithCommented:
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!!
0
 
SteviusAuthor Commented:

HAd to do something else then asp.
strange but true: Undefined function 'getdate' in expression.
Anyone knows what to do?
Thanks ;)
0
 
ap_sajithCommented:
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!!
0
 
SteviusAuthor Commented:
! 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 ...
0
 
SteviusAuthor Commented:
When I try Date() I get the message that no value is given for one or more parameters
0
 
ap_sajithCommented:
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!!
0
 
SteviusAuthor Commented:
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.

0
 
ap_sajithCommented:
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!!
0
 
SteviusAuthor Commented:
Hey ap_sajith,

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

Thanks !
0
 
SteviusAuthor Commented:
So can only be used within sql
DB still is Access  -  I'm thinking about transfer to SQL.


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

Cheers!!
0
 
ap_sajithCommented:
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!!
0
 
SteviusAuthor Commented:
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 15
  • 13
  • 12
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now