VARCHAR considerations in DB2.

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
Ankit Kumar Gupta
Registered Member
Posts: 19
Joined: Wed Aug 20, 2014 5:24 pm

VARCHAR considerations in DB2.

Post by Ankit Kumar Gupta »

Hi,

In COBOL-SQL program, while inserting data into a VARCHAR field, is it necessary to populate the length host variable, before insert?

Is not it that DB2 automatically stores the length?
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: VARCHAR considerations in DB2.

Post by zprogrammer »

Hi Ankit,

Db2 automatically pads for the whole length and does not save space , which is not needed and also breaks the purpose of having a varchar field ...

So it is better to pass the length of the data before inserting into the table
zprogrammer
Ankit Kumar Gupta
Registered Member
Posts: 19
Joined: Wed Aug 20, 2014 5:24 pm

Re: VARCHAR considerations in DB2.

Post by Ankit Kumar Gupta »

Pandora-Box wrote:Db2 automatically pads for the whole length and does not save space , which is not needed and also breaks the purpose of having a varchar field ...
What re you telling here Pandora-Box, sorry I did not get you?
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: VARCHAR considerations in DB2.

Post by zprogrammer »

For If have a FIELD VARCHAR(40) and try to insert 'Pandora' using host variable without mentioning the length of the data what happens is it pads extra 33 spaces to 'pandora' and stores in table

so there no use in having a varchar if you are going to use the 40 bytes instead if you pass the length which 7 to the length field and store only the actual data is stored
zprogrammer
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: VARCHAR considerations in DB2.

Post by Anuj Dhawan »

Ankit Kumar Gupta wrote:In COBOL-SQL program, while inserting data into a VARCHAR field, is it necessary to populate the length host variable, before insert?
Yes.
Is not it that DB2 automatically stores the length?
Yes and no. DB2 will pick up the length of the column defined as VARCHAR. If the data in the column is always of maximum length as of column, well that's the length (but that beats the purpose to define the column as VARCHAR), however if the data is not of same length as column-length - DB2 won't determine it its own and you've to tell it to DB2.

Hope this helps.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”