T-SQL: Is there a better way to write 'where column_name like '%CS?

Hi Experts,
I would like to do an update. What would be the best way to do it if mail_cd is always 5 characters and I'd like to capture the last two characters only? This is how I'm doing it now but I'm curious if there is a better way to do it:

update Employees set Department ='Customer Service' where mail_cd like '%CS'
romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
That way is fine, and wold be better performance only if Department and mail_cd were indexed.

Otherwise, RIGHT(mail_cd, 2) works, SUBSTRING(mail_cd, 4, 2) works too.

>mail_cd is always 5 characters
Might not be a bad ideal to run the below just to make sure:

SELECT mail_cd FROM Employees WHERE LEN(mail_cd) <> 5
0
ste5anSenior DeveloperCommented:
Nope, as long as you don't want to use indices.

In this case create a persisted, computed colum of REVERSE(mail_cd) and create an index on this column. Then search for
 WHERE reversed_mail_cd LIKE REVERSE('%CS').

Otherwise is not sargeable.
0
Russ SuterCommented:
Your method works OK. An alternative would be:

UPDATE
  Employees
SET
 Department = 'Customer Service'
WHERE
 RIGHT(mail_cd, 2) = 'CS'

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
ste5an - Does that mean that LIKE '%CS' is not SARGable, but LIKE 'CS%' is?  
If so, good to know, learn something new every day..
0
romsomIT DeveloperAuthor Commented:
Thank you all!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the split.  Good luck with your code.  -Jim
0
ste5anSenior DeveloperCommented:
@Jim: Yup, LIKE 'xyz%' can use an index seek.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.