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

Join the conversation

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

Guest
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...