Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 02:17 PM
Lord Merlin
Guest
 
Posts: n/a
Default checking record older than 2 years not working?

Good day to you all

I'm trying to see all the records in the database older than 2 years from
today, and I can't seem to be getting it to go.
SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20) ORDER BY
currentdate

I get null records returned, even though I can see some records as old as
1995.
When I run the command like this however, it returns all the rows.
SELECT * FROM comments WHERE (currentdate > 2002 - 07 - 20) ORDER BY
currentdate

any suggestions?
--


Kind Regards
Rudi Ahlers
+27 (82) 926 1689

Greater love has no one than this, that he lay down his life for his friends
(John 15:13).


  #2  
Old July 19th, 2005, 02:17 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: checking record older than 2 years not working?

Lord Merlin wrote:[color=blue]
> Good day to you all
>
> I'm trying to see all the records in the database older than 2 years
> from today, and I can't seem to be getting it to go.
> SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20) ORDER
> BY currentdate
>
> I get null records returned, even though I can see some records as
> old as 1995.
> When I run the command like this however, it returns all the rows.
> SELECT * FROM comments WHERE (currentdate > 2002 - 07 - 20) ORDER
> BY currentdate
>
> any suggestions?[/color]

Yes, tell us the type and version of database you are using, and tell us the
datatype of currentdate.

Bob Barrows

PS. .components is not relevant. Why did you crosspost to that group? .db is
the only really relevant group in your list.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


  #3  
Old July 19th, 2005, 02:17 PM
Lord Merlin
Guest
 
Posts: n/a
Default Re: checking record older than 2 years not working?

"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:OYwJ6SebEHA.3012@tk2msftngp13.phx.gbl...
| Lord Merlin wrote:
| > Good day to you all
| >
| > I'm trying to see all the records in the database older than 2 years
| > from today, and I can't seem to be getting it to go.
| > SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20) ORDER
| > BY currentdate
| >
| > I get null records returned, even though I can see some records as
| > old as 1995.
| > When I run the command like this however, it returns all the rows.
| > SELECT * FROM comments WHERE (currentdate > 2002 - 07 - 20) ORDER
| > BY currentdate
| >
| > any suggestions?
|
| Yes, tell us the type and version of database you are using, and tell us
the
| datatype of currentdate.
|
| Bob Barrows
|
| PS. .components is not relevant. Why did you crosspost to that group? .db
is
| the only really relevant group in your list.
| --
| Microsoft MVP - ASP/ASP.NET
| Please reply to the newsgroup. This email account is my spam trap so I
| don't check it very often. If you must reply off-line, then remove the
| "NO SPAM"
|
|
The server is MS SQL 2000, and the data type is datetime. Does this make a
big difference if the databases I use are generally SQL, i.e SQL 7 / SQL
2000 / MySQL / Access?

--


Kind Regards
Rudi Ahlers
+27 (82) 926 1689

Greater love has no one than this, that he lay down his life for his friends
(John 15:13).


  #4  
Old July 19th, 2005, 02:17 PM
Bob Barrows [MVP]
Guest
 
Posts: n/a
Default Re: checking record older than 2 years not working?

Lord Merlin wrote:[color=blue]
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:OYwJ6SebEHA.3012@tk2msftngp13.phx.gbl...[color=green]
>> Lord Merlin wrote:[color=darkred]
>>> Good day to you all
>>>
>>> I'm trying to see all the records in the database older than 2 years
>>> from today, and I can't seem to be getting it to go.
>>> SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20)
>>> ORDER BY currentdate
>>>
>>> I get null records returned, even though I can see some records as
>>> old as 1995.
>>> When I run the command like this however, it returns all the rows.
>>> SELECT * FROM comments WHERE (currentdate > 2002 - 07 - 20)
>>> ORDER BY currentdate
>>>
>>> any suggestions?[/color]
>>
>> Yes, tell us the type and version of database you are using, and
>> tell us the datatype of currentdate.
>>[/color]
> The server is MS SQL 2000, and the data type is datetime. Does this
> make a big difference if the databases I use are generally SQL, i.e
> SQL 7 / SQL 2000 / MySQL / Access?[/color]

Of course it does! Access uses JetSQL (and VBA functions). SQL Server uses
T-SQL. I have no idea what MySQL uses.

The answer to any query question will ALWAYS depend on the type (and
sometimes the version) of database you are using. Do not keep it a secret.

Anyways, for SQL2000 (and earlier):

SELECT <list of columns> FROM comments
WHERE currentdate <= DATEADD(yy,-2,GETDATE())
ORDER BY currentdate

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


  #5  
Old July 19th, 2005, 02:18 PM
Lord Merlin
Guest
 
Posts: n/a
Default Re: checking record older than 2 years not working?


Greater love has no one than this, that he lay down his life for his friends
(John 15:13).
"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:uql4X2ebEHA.3888@TK2MSFTNGP10.phx.gbl...
| Lord Merlin wrote:
| > "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
| > news:OYwJ6SebEHA.3012@tk2msftngp13.phx.gbl...
| >> Lord Merlin wrote:
| >>> Good day to you all
| >>>
| >>> I'm trying to see all the records in the database older than 2 years
| >>> from today, and I can't seem to be getting it to go.
| >>> SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20)
| >>> ORDER BY currentdate
| >>>
| >>> I get null records returned, even though I can see some records as
| >>> old as 1995.
| >>> When I run the command like this however, it returns all the rows.
| >>> SELECT * FROM comments WHERE (currentdate > 2002 - 07 - 20)
| >>> ORDER BY currentdate
| >>>
| >>> any suggestions?
| >>
| >> Yes, tell us the type and version of database you are using, and
| >> tell us the datatype of currentdate.
| >>
| > The server is MS SQL 2000, and the data type is datetime. Does this
| > make a big difference if the databases I use are generally SQL, i.e
| > SQL 7 / SQL 2000 / MySQL / Access?
|
| Of course it does! Access uses JetSQL (and VBA functions). SQL Server uses
| T-SQL. I have no idea what MySQL uses.
|
| The answer to any query question will ALWAYS depend on the type (and
| sometimes the version) of database you are using. Do not keep it a secret.
|
| Anyways, for SQL2000 (and earlier):
|
| SELECT <list of columns> FROM comments
| WHERE currentdate <= DATEADD(yy,-2,GETDATE())
| ORDER BY currentdate
|
| Bob Barrows
| --
| Microsoft MVP - ASP/ASP.NET
| Please reply to the newsgroup. This email account is my spam trap so I
| don't check it very often. If you must reply off-line, then remove the
| "NO SPAM"
|
|

Thanx :)



--


Kind Regards
Rudi Ahlers
+27 (82) 926 1689


  #6  
Old July 19th, 2005, 02:18 PM
Aaron [SQL Server MVP]
Guest
 
Posts: n/a
Default Re: checking record older than 2 years not working?

> SELECT * FROM comments WHERE (currentdate < 2002 - 07 - 20) ORDER BY[color=blue]
> currentdate[/color]

Since you have no delimiters around your oddly-formatted "date", this
becomes an expression, and your query is equivalent to:

SELECT * FROM comments WHERE (currentdate < 1975) ORDER BY currentdate
(Note, that is still a numeric expression, not a date or a year.)

Which is equivalent to:

SELECT * FROM comments WHERE (currentdate < '19050530') ORDER BY currentdate

In addition to the string delimiter problem, you shouldn't be hard-coding
the date into the query at all. Assuming you always want rows more than two
years old, here is how your query *should* be formatted:

SELECT <column_list>
FROM comments
WHERE (currentdate < DATEADD(YEAR, -2, CONVERT(CHAR(8), GETDATE(),
112)))
ORDER BY currentdate


Also, don't use SELECT *:
http://www.aspfaq.com/2096

Follow-ups set to asp.db only. This isn't a components issue, and belongs
only in the db-related group. Just because the group has "asp" in the name
doesn't make your question relevant there.

--
http://www.aspfaq.com/
(Reverse address to reply.)


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles