TSQL - String Changes

Hi Experts,

I have this and it is an Access Query:
iif(Left([companyName],4)="The ",mid([companyName],4) & ", The",[companyName])

Open in new window


What it does is looks for a name that starts with "The" it takes it out from the front and puts it at the end so:

The Book Store
Would be:
Book Store The

I need to use TSQL to do the same thing and need help writing the proper syntax for TSQL which will be an update to the table.

TableName.CompanyName

Please help and thanks....
Amour22015Asked:
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 Data DudeCommented:
SELECT CASE WHEN LEFT(companyName, 4) = 'The' THEN RIGHT(companyName, LEN(CompanyName) - 4)) + '  The' ELSE CompanyName END as companyName
FROM YourTable

Open in new window

To test, copy-paste this in SSMS and execute
CREATE TABLE #tmp (companyName varchar(100)) 

INSERT INTO #tmp (companyName) 
VALUES ('The Rumpus Room'), ('Adam 12'), ('Surf Nazis Must Die'), ('Starship Troopers'), ('The Hunger Games')

SELECT CASE WHEN LEFT(companyName, 4) = 'The' THEN RIGHT(companyName, LEN(CompanyName) - 4) + ' The' ELSE companyName END as companyName
FROM #tmp

Open in new window

Fore more Access SQL to SQL Server T-SQL converstion tips check out my article at Migrating your Access Queries to SQL Server Transact-SQL

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
Amour22015Author Commented:
It looks like I will have to do an update.

So I am looking for something like:

Update YourTable
Set Search1 = ????
From YourTable
Where ?????

Can I do an update to the table in question like that?

Thanks for helping....
Amour22015Author Commented:
Maybe something like:
Update YourTable
Set Search1 = CASE WHEN LEFT(companyName, 4) = 'The' THEN RIGHT(companyName, LEN(CompanyName) - 4)) + '  The' ELSE CompanyName END as companyName
From YourTable
Where CompanyName Like 'The %'

Thanks for helping
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jim HornMicrosoft SQL Server Data DudeCommented:
Same code but UPDATE instead of SELECT...
UPDATE YourTable 
SET CompanyName = CASE WHEN LEFT(companyName, 4) = 'The' THEN RIGHT(companyName, LEN(CompanyName) - 4)) + '  The' ELSE CompanyName END

Open in new window


CREATE TABLE #tmp (companyName varchar(100)) 

INSERT INTO #tmp (companyName) 
VALUES ('The Rumpus Room'), ('Adam 12'), ('Surf Nazis Must Die'), ('Starship Troopers'), ('The Hunger Games')

SELECT 'Before', companyName FROM #tmp

UPDATE #tmp
SET CompanyName = CASE WHEN LEFT(companyName, 4) = 'The' THEN RIGHT(companyName, LEN(CompanyName) - 4) + ' The' ELSE companyName END

SELECT 'After', companyName FROM #tmp

Open in new window

Fore more Access SQL to SQL Server T-SQL converstion tips check out my article at Migrating your Access Queries to SQL Server Transact-SQL
Amour22015Author Commented:
Also on the Access Query:
iif(Left([companyName],4)="The ",mid([companyName],4) & ", The",[companyName])

Open in new window


I just notice that it puts a blank in front So like:

The Book Store

Book Store, The

But it comes out as:
 Book Store, The

Please help and thanks
Amour22015Author Commented:
Great Thanks
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 Access

From novice to tech pro — start learning today.