Jump to content

Substring Syntax for Limiting Company Name Lengths


Recommended Posts

  • Administrators

Some time ago I was reading data from a flat .txt file and inserting it into my database. My table in the database was only setup to handle 40 characters for the company or person's last name. If it was a company some would be longer than 40 and my job would fail. I chose to fix it with the substring function in MSSQL.


Table: Debtor

Field: RespLName


My original code:

select case when ltrim(rtrim(RespLName)) is null then P.LastName else ltrim(rtrim(RespLName)) end as LastName
from Debtor


After adding the substring function:

select case when ltrim(rtrim(substring(RespLName,0,40))) is null then P.LastName else ltrim(rtrim(substring(RespLName,0,40))) end as LastName
from Debtor

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...