Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with updating records in a column.


Help with updating records in a column.

Author
Message
anushakadiyala 52521
anushakadiyala 52521
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 32
Hello,

I am completely new to T-SQL and I am learning how to do some complex stuff.

I have a table called TA with columns ServerName, TextA


Table Name : TA
columns 1: Server name
column 2: TextA


in the column "ServerName" I have listed server names like

Serv1 Prod
Serv2 prod
Server3.disc.dim.com prod
server4.dim.com Prod
server5.st.dim.com prod


I need to update the column Servername such a way that I need to remove anything with ".disc.dim.com",".dim.com","st.dim.com"

I tried using replace function but that doesn't seem to help.

Any guidance is much appreciated.


Regards,

Anush.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47204 Visits: 44367
What was the code that you tried?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


anushakadiyala 52521
anushakadiyala 52521
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 32
Thank you for taking a look, This is the code I have been trying.

UPDATE TA
SET ServerName= REPLACE(ServerName, '%.%.dim.com', '')
WHERE Servername like '%.%.dim.com%'


basically I want to remove anything that starts with first "."
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16548 Visits: 17004
anushakadiyala 52521 (12/17/2013)
Thank you for taking a look, This is the code I have been trying.

UPDATE TA
SET ServerName= REPLACE(ServerName, '%.%.dim.com', '')
WHERE Servername like '%.%.dim.com%'


basically I want to remove anything that starts with first "."


I think what you are trying to do is to change the ServerName to be dim.com? They way you have this coded it won't actually do anything.

Is this closer to what you want?


UPDATE TA
SET ServerName= 'dim.com'
WHERE Servername like '%.%.dim.com%'



This still probably isn't quite what you are looking for but it is not at all clear what you are trying to do.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45013 Visits: 39886
anushakadiyala 52521 (12/17/2013)
Thank you for taking a look, This is the code I have been trying.

UPDATE TA
SET ServerName= REPLACE(ServerName, '%.%.dim.com', '')
WHERE Servername like '%.%.dim.com%'


You cannot use wildcards in a REPLACE.


basically I want to remove anything that starts with first "."


The following should do as you ask... I don't know how you operate but I recommend not just blindly using this. Lookup the functions in Books Online (press the {f1} key in SSMS to get there) and teach yourself how they work and why. ;-)

 UPDATE dbo.TA
SET ServerName = SUBSTRING(ServerName,1,CHARINDEX('.',ServerName)-1)
WHERE ServerName LIKE '%.%'
;



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18115
As far as I understand, it should be more like this to keep the "prod" at the end of the name.

 UPDATE TA
SET ServerName = STUFF(ServerName, CHARINDEX('.', Servername), CHARINDEX(' ', Servername) - CHARINDEX('.', Servername), '')
WHERE ServerName LIKE '%.%'




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45013 Visits: 39886
Luis Cazares (12/17/2013)
As far as I understand, it should be more like this to keep the "prod" at the end of the name.

 UPDATE TA
SET ServerName = STUFF(ServerName, CHARINDEX('.', Servername), CHARINDEX(' ', Servername) - CHARINDEX('.', Servername), '')
WHERE ServerName LIKE '%.%'



Hmmm... I wasn't even looking at the code examples. I was going only by...
basically I want to remove anything that starts with first "."


The code examples do tell a different story, though. Thanks for the catch.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search