Jump to content
Nathan

MSSQL Substring Syntax for Limiting Company Name Lengths

Recommended Posts

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

Share this post


Link to post
Share on other sites

Nice one Nathan!

 

As a suggestion that could be made simpler:

 

select IsNull(LTrim(RTrim(SubString(RespLName,0,40))),P.LastName)
from Debtor

Edited by __Darknite

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×