How to treat a CHAR like a VARCHAR

Use this procedure to get the length of actual text (not spaces) of a row in a CHAR column returned in CHAR format.

If you need to have the length of actual text (not spaces) of a row in a CHAR column returned in char format. This...

is particularly handy when concatinating data together with a value containing its length. I found it useful for writing from a trigger to a table including the length of the data. It has been tested on DB2 V7 for OS/390. This tip is based upon work by David Corrigan.

'EMAIL ADDRESS IS '||
STRIP(CHAR(LENGTH(RTRIM(EMAIL_ADDR))),B)
' CHARACTERS LONG'

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free DB2 tips and scripts.
  • Tip contest: Have a DB2 tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical DB2 questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: DB2 tips, tutorials, and scripts from around the Web.

This was last published in December 2002

Dig Deeper on DB2 UDB (universal databases)

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close