Advertisement

02.15.2008 at 02:56PM PST, ID: 23167587
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

UDB DB2 Database --- select MAX value form CHAR(20) datatype field

Zone: DB2 Database
Tags: UDB DB2 DATABASE
In DB2 i have a data field char(20)
when i execute the following SQL
select fieldname from tablename
1234
abc12ab123
12ab23c23d
ab1234cd
cd1234
42345
67890

when i do select max(fieldname) from tablename

Result should be 67890

i tried many ways but i'm unable to get the result.
I would appreciate if someone can help me..

Thanks
Start your free trial to view this solution
Question Stats
Zone: Database
Question Asked By: amrex2k
Solution Provided By: momi_sabag
Participating Experts: 3
Solution Grade: A
Views: 166
Translate:
Loading Advertisement...
02.15.2008 at 03:29PM PST, ID: 20906987

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 04:20PM PST, ID: 20907216

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 04:58PM PST, ID: 20907373

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 04:59PM PST, ID: 20907384

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.16.2008 at 10:02AM PST, ID: 20910761

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.16.2008 at 02:32PM PST, ID: 20911629

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.15.2008 at 03:29PM PST, ID: 20906987

Rank: Master


Since I assume your field is alphanumeric, how exactly is 67890 greater than the other rows?

If you want the maximum NUMERIC value of a character column, you'll have to filter out the rows with non-numeric data.

e.g.
select max(field1)          
from   deleteme            
where  IsNumeric(field1) = 1

I'll attach my code for the IsNumeric function.

HTH,
DaveSlash
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
CREATE FUNCTION IsNumeric (
    inString VARCHAR(100)
)
  RETURNS SmallInt
  LANGUAGE SQL
  modifies sql data
 
BEGIN
-- Scratch variables used for processing
DECLARE stringLength INT;
DECLARE loopCounter INT;
DECLARE charAtPos VARCHAR(1);
 
-- If the incoming string is NULL, return an error
IF (inString IS NULL) THEN
 RETURN 0;
End If;
 
 -- Initialize the scratch variables
 SET stringLength = LENGTH(inString);
 SET loopCounter = 1;
 
 -- Loop over the string
 WHILE (loopCounter <= stringLength) DO
    -- Get the single character off the string
    SET charAtPos = LOWER(SUBSTRING (inString, loopCounter, 1));
 
    IF ((CharAtPos < '0') or (CharAtPos > '9')) then
       RETURN 0;
    END IF;
 
   SET loopCounter = loopCounter + 1;
END WHILE;
 
-- Return the final output
RETURN 1;
END
Open in New Window
 
02.15.2008 at 04:20PM PST, ID: 20907216
Thanks, I have modified changed the datatypes as per db2 and was able to built the function. But when i try to run i get the following error:

IsNumericTEST - Run started.
IsNumericTEST - Exception occurred while running:
[IBM][CLI Driver][DB2/6000] SQL0440N  No authorized routine named "ISNUMERICTEST" of type "FUNCTION" having compatible arguments was found.  SQLSTATE=42884
IsNumericTEST - Roll back completed successfully.
IsNumericTEST - Run failed.

I appreciate for any suggestions. thanks

CREATE FUNCTION IsNumeric (inString VARCHAR(2))
  RETURNS INTEGER;
 -- LANGUAGE SQL
--  modifies sql data
BEGIN ATOMIC
-- Scratch variables used for processing
DECLARE stringLength INTEGER;
DECLARE loopCounter INTEGER;
DECLARE charAtPos VARCHAR(1);

-- If the incoming string is NULL, return an error
IF (inString IS NULL) THEN
 RETURN 0;
End If;

 -- Initialize the scratch variables
 SET stringLength = LENGTH(inString);
 SET loopCounter = 1;

 -- Loop over the string
 WHILE (loopCounter <= stringLength) DO
    -- Get the single character off the string
    SET charAtPos = LOWER(SUBSTR(inString, loopCounter, 1));

    IF ((CharAtPos < '0') or (CharAtPos > '9')) then
       RETURN 0;
    END IF;

   SET loopCounter = loopCounter + 1;
END WHILE;

-- Return the final output
RETURN 1;
END    
 
02.15.2008 at 04:58PM PST, ID: 20907373

Rank: Master


Did the error come when creating the function or when using it in a select statement?

If it came when using it, please post the select statement.

-- DaveSlash
 
02.15.2008 at 04:59PM PST, ID: 20907384

Rank: Master


Also, it looks like you defined the input parameter to have a length of 2.  Therefore, most of the data you posted in your original question wouldn't work in this function (since they have lengths much greater than 2).

-- DaveSlash
 
02.16.2008 at 10:02AM PST, ID: 20910761

Rank: Wizard

you can achieve this without a function
if you want to get the numbers sorted before the character letters (which is not defualt behaviour)
you can just exclude the characters like this :
select fieldname from tablename
where fieldname < 'aaaaaaaaaaaaaaaaaaaa'
this way it will filter out all the values that begin with a character and not with a digit

note that the order by sequnece is determined by the ccsid you choose, so you might see differences between ebcdic, ascii and unicode
Accepted Solution
 
02.16.2008 at 02:32PM PST, ID: 20911629

Rank: Wizard


Hi momi,

That sort won't work for embedded or trailing characters. '0a14'  sorts higher but is obviously not an integer.

You can search with a couple of built-in functions though

  select max (field)
  from ....
  where trim (translate (field, '', '0123456789')) = '';


Good Luck,
Kent
 
 
Kdo
02.18.2008 at 11:16AM PST, ID: 20922560

Sorry Amrex,

But that solution simply doesn't meet your requirements.



Kent
 
 
02.18.2008 at 01:44PM PST, ID: 20923748
Kdo,

I tried
select max (fieldname) from schemaname.tablename where trim(translate (fieldname, '', '0123456789')) = '';
In db2 trim don't work.. so i used ltrim..

Could you please tell me if you know any better solutions which works in DB2...

Thanks
 
 
Kdo
02.18.2008 at 02:18PM PST, ID: 20924036

Hi amrex,

I was using trim() in a generic sense as the trim, rtrim, and ltrim functions aren't available universally.  As I recall, the ltrim and rtrim functions are for DB2/LUW, and the trim function in DB2 for AS/400.

Another possible complication is whether the converted string is evaluated as a char() or varchar().  If char(), you need to compare to the correct length string.  If varchar, any number of spaces will do.

As such, try this:

select max (field)
from ....
where cast ((translate (field, '', '0123456789')) to varchar(20)) = '';


Kent

And of course, Dave's solution works fine.  It's just a bit more work than you should have to do.

 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628