Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Nov 16, 2018 6:09 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 2:58 pm 
Offline

Joined: Sat Feb 04, 2012 1:26 pm
Posts: 86
Location: Italy
Hi
I have database with the column "data_1" containing dates; but not all the records contain dates. So i performed the following select extraction:
rs=db.SQLSelect("SELECT data_1  FROM [db 2012] where data_1 IS NOT NULL")

and it works without any problem.

But what i really need is to extract a single value representing the most recent date contained in the database, so i performed the
following select extraction, adding the Max function :
rs=db.SQLSelect("SELECT MAX(data_1)  FROM [db 2012] where data_1 IS NOT NULL")

... it does not works and
returns me a Nilobjectexception message.

i also tried the following:
rs=db.SQLSelect("SELECT max(data_1) IN (select data_1 FROM [db 2012] where data_1 IS NOT NULL)")

...without success.




Any suggestions ?


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 3:56 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
What data type is DATA_1?
if not a DATE/TIME datatype.. what format is it in?
What database engine?
Show some samples of what DATA_1 looks like

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 4:22 pm 
Offline

Joined: Sat Feb 04, 2012 1:26 pm
Posts: 86
Location: Italy
Thanks for helping, Dave.

Data_1 column is in DATE/TIME Datatype format.
Name Column always contains a name. Data_1 may contain a date.
The Database Engine is Access; *.mdb file.



Name | Data_1

Antony | 2012/11/29
Mike |
Mary | 2010/02/04
John | 2011/08/01


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 4:25 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
try this

SELECT MAX(data_1) as data_1 FROM [db 2012] WHERE data_1 IS NOT NULL


That SHOULD work (all I did was add an alias to the MAX)

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 4:40 pm 
Offline

Joined: Sat Feb 04, 2012 1:26 pm
Posts: 86
Location: Italy
Dave,
it doesn't work

it returns me database error -3005.

I also tryed to modify the alias from data_acc to data_acctry and it returns again the ....nilobjectexception was not handled..


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 4:54 pm 
Offline

Joined: Sat Feb 04, 2012 1:26 pm
Posts: 86
Location: Italy
It works, It works.

I had to modify the second part of the code: label1.text=rs.field....

but why it did not work without the alias?


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 5:27 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Without the alias, the column returned is literally named "max(data_1)", not "data_1". So if you try to access it by name,

rs.field("data_1")

will return nil, because the recordset does not contain a column named data_1, it contains a column named max(data_1). The aliasw resolves this issue.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 5:31 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
BTW, you shouldn't need the WHERE clause in your selection. According to the sql standard, MAX() ignores null values anyway.


Top
 Profile  
Reply with quote  
 Post subject: Re: Sql Max(date) doesn't work. need help
PostPosted: Thu Nov 29, 2012 5:46 pm 
Offline

Joined: Sat Feb 04, 2012 1:26 pm
Posts: 86
Location: Italy
Dave, Tim,
thank you very much for help me to improve.
ciao,
Antonio


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group