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 > LENGTH function returns bad values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-09, 14:56
curtmorehouse curtmorehouse is offline
Registered User
 
Join Date: Sep 2008
Posts: 27
LENGTH function returns bad values

I have a field, MDTIME, that shows raw values as such...
124306
75534
100508
2502
134015

etc.. but when I use LENGTH(MDTIME) I get 6 for every row.

This made me think the values were acutally as such...
124306
075534
100508
002502
134015

with leading zeros somehow not showing, but actually there in the tables.

so then I did SUBSTR(MDTIME,1,2) and not once did I get a 0 (zero) as a first character. IS there something wrong with LENGTH? Is it telling me the LENGTH of the field or the string in it? I want the LENGTH of the string in it if that's possible. thanks for any help you can offer.
Reply With Quote
  #2 (permalink)  
Old 04-17-09, 15:07
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
if the filed is char and must be because substr is being used, all characters are displayed in select try
select mdtime,length(mdime),hex(mdtime) from ..
because this is char(6) the length is always 6
where do you display data in command line or other application. eg excel would remove leading zeroes
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 04-17-09, 23:17
nick.ncs nick.ncs is offline
Registered User
 
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
That is because you have defined the field as CHAR(6). To get variable field length you'll have to define your column as VARCHAR(6) instead.
__________________
IBM Certified Database Associate, DB2 9 for LUW
Reply With Quote
  #4 (permalink)  
Old 04-20-09, 14:44
curtmorehouse curtmorehouse is offline
Registered User
 
Join Date: Sep 2008
Posts: 27
solved

Thanks for the help! You were right about the field type being CHAR instead of VARCHAR. I ended up using SUBSTR(DIGITS(MDTIME),1,4) to get the time from that field (I don't need the seconds, that's why it's 1,4 instead of 1,6)
Reply With Quote
  #5 (permalink)  
Old 04-21-09, 02:24
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
if using time data why don't you use time data type?
Regards
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