If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sql please help !!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-30-04, 06:43
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
Sql please help !!

For example, This is the table name microprice in Microsoft Access.

dat price
8/23/2004 11
8/24/2004 28
8/25/2004 13
8/26/2004 24
8/27/2004 56
8/28/2004 12
8/29/2004 32
8/30/2004 21

I want to show the last five price not include the date of today. Assume today is 8/30/2004

I want to write the sql that show
8/25/2004 13
8/26/2004 24
8/27/2004 56
8/28/2004 12
8/29/2004 32

So how can I write SQl. I try to use
sql="select top 5 * from microprice where dat <> '%"&date()&"%' order by dat desc"

But it doesn't work !!!
Reply With Quote
  #2 (permalink)  
Old 08-30-04, 06:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
where dat <> date()
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-30-04, 08:56
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
I have already tried. But it doesn't work!!! It still shows the date of today.
Reply With Quote
  #4 (permalink)  
Old 08-30-04, 09:00
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
Ok. It works now.
It must be
"select top 5 * from microprice where dat not like '%"&date()&"%' order by dat desc "
Reply With Quote
  #5 (permalink)  
Old 08-30-04, 09:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
ok, the date values must have time components

try where dat < date()
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 08-30-04, 09:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
like '%"&date()&"%' treats the dat value as a string

if this "works" it is not efficient, and will disregard any index on dat

furthermore, it relies on the default access display format being the same format as your default scripting language date() function

the solutions i gave you use the builtin access database date() function, thus treating dat values as a datetime values
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 09-01-04, 09:06
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
I tried but it does not work !!
Reply With Quote
  #8 (permalink)  
Old 09-01-04, 09:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what did you try, and what did it produce?

what is the datatype of your date column?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-01-04, 10:07
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
I tried
sql="select top 5 * from microprice where dat < date() order by dat desc"
But it is not work.

My datatype of date column is Text
Because the first time I use date/time but there is nothing. But when I change into text and use

sql="select top 5 * from microprice where dat not like '%"&date()&"%' order by dat desc "

It work
Reply With Quote
  #10 (permalink)  
Old 09-01-04, 13:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
TEXT is a poor choice in comparison to DATE/TIME

good luck and have fun
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On