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 > Database Server Software > DB2 > troubled with a simple SELECT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-25-06, 17:12
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
troubled with a simple SELECT

Hi All
I am using DB2 V8.1 on linux.
I have a table as below:

Code:
 
CREATE TABLE AL_TABLE_EXT (LEN INT, TEXT COLB (2M));
Column LEN is the actual length of TEXT. Now I need to return TEXT with trailing spaces truncated. If LEN = 100, What I need is EXACTLY the result of following:

Code:
SELECT LEN, SUBSTR(TEXT,1,100) AS TEXT FROM AL_TABLE_EXT;
I definitely can not hard-code 100 in my application, so I tried the following:

Code:
 
SELECT LEN, SUBSTR(TEXT,1,LEN) AS TEXT FROM AL_TABLE_EXT;
The second one did not give me a sub-string, but a whole length of string (with trailing spaces). I also tried function RTRIM. The result was the same.
It seems a simple thing, but I just can't make it exactly as I need.

Could you please advise me? Thank you in advance.
Reply With Quote
  #2 (permalink)  
Old 07-28-06, 13:09
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Try this:
Code:
SELECT LEN, CAST(SUBSTR(TEXT,1,LEN) AS VARCHAR) AS TEXT FROM AL_TABLE_EXT
(I did not check its working, though.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #3 (permalink)  
Old 07-28-06, 18:28
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
Thanks Peter. But this one still didn't work.
Reply With Quote
  #4 (permalink)  
Old 07-29-06, 08:27
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
After some trial-and-error, the following seems to work with DB2 v8.1 for Windows:
Code:
select cast(rtrim(text) AS varchar(32672)) from al_table_ext
or alternatively
Code:
select cast(substr(text,1,len) AS varchar(32672)) from al_table_ext
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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