Page 1 of 1

Get the first 5 characters of a column in DB2?

Posted: Mon Dec 21, 2015 1:56 pm
by Suresh R
Hi,

How can we get the first 5 characters of a column in DB2? Interviewer explained it again saying, how would you get the first 5 characters of say, FIRSTNAME column of DB2 table STUDENT?

Please help how to do this?

Re: Get the first 5 characters of a column in DB2?

Posted: Mon Dec 21, 2015 2:05 pm
by Chandan Yadav
Hi,

Check for SUBSTR function of Db2

Regards,
Chandan

Re: Get the first 5 characters of a column in DB2?

Posted: Mon Dec 28, 2015 3:07 pm
by Suresh R
Thanks Chandan. Examples in this link http://www-01.ibm.com/support/knowledge ... ubstr.dita has helped me. Giving it here for reference:

Example 1: FIRSTNME is a VARCHAR(12) column in sample table DSN8A10.EMP. When FIRSTNME has the value 'MAUDE':

SUBSTR(FIRSTNME,2,3) -- 'AUD'
SUBSTR(FIRSTNME,2) -- 'AUDE'
SUBSTR(FIRSTNME,2,6) -- 'AUDE' followed by two blanks
SUBSTR(FIRSTNME,6) -- a zero-length string
SUBSTR(FIRSTNME,6,4) -- four blanks

Re: Get the first 5 characters of a column in DB2?

Posted: Wed Dec 30, 2015 10:32 am
by zprogrammer
Thanks for sharing the link , It would help people future :)