Administrators Nathan Posted May 4, 2012 Administrators Share Posted May 4, 2012 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 Quote Link to comment Share on other sites More sharing options...
__Darknite Posted May 23, 2012 Share Posted May 23, 2012 (edited) Nice one Nathan! As a suggestion that could be made simpler: select IsNull(LTrim(RTrim(SubString(RespLName,0,40))),P.LastName) from Debtor Edited May 23, 2012 by __Darknite Nathan 1 Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted May 23, 2012 Author Administrators Share Posted May 23, 2012 Ah, good call, didn't even think about isnull, guess that would be more efficient than the case statement. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.