Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

weird update statement Expand / Collapse
Author
Message
Posted Monday, September 10, 2012 1:08 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 17, 2014 7:01 PM
Points: 91, Visits: 464
I have a table name Test_name.which has two columns full_name and nick_name.I have values only in full_name where as Nick_name column is blank which i need to update as per full_name.I have more than 1000 distinct full_name.I want to update the ccolumn according to different logic which all depends upon underscore(_) in full_name.to illustrate i have full_name= 'james_9333_mathew_rohn' it means full_name has more than 1 underscore in name then i want nick_name to be ''james_rohn'.but if there is underscore at last in full_name( 'james_9333_mathew_rohn_' ) then result could be either 'james_rohn' or 'james_rohn_' both are fine for this case.when we have only 1 undersore like 'james_rohn' then nick_name should be 'james_rohn' and but if there is undersocre at last like 'james_rohn_' then result could be either either 'james_rohn' or 'james_rohn_' both are fine.please help me to solve this.
'
create table test_name( full_name varchar(50),Nick_Name varchar(50))
insert into test_name values( 'james_9333_mathew_rohn','')
,('james_9333_mathew_rohn_',''),
('james_rohn',''),
('james_rohn_','')

select * from test_name

update test_name
set Nick_Name=

case when full_name='james_9333_mathew_rohn' then 'james_rohn'
when full_name='james_9333_mathew_rohn_' then 'james_rohn_'
when full_name='james_rohn' then 'james_rohn'
when full_name='james_rohn_' then 'james_rohn_'
end

select * from test_name

Post #1356968
Posted Monday, September 10, 2012 1:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
Nice job posting ddl and sample data. Unfortunately it is totally unclear what you are trying to do. Can you provide a clear explanation of your requirements. Additionally if you could post your desired results based on your sample data that would help.

_______________________________________________________________

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)
Post #1356978
Posted Monday, September 10, 2012 1:33 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 17, 2014 7:01 PM
Points: 91, Visits: 464
I have a table name Test_name.which has two columns full_name and nick_name.I have values only in full_name where as Nick_name column is blank which i need to update as per full_name.I have more than 1000 distinct full_name.I want to update the ccolumn according to different logic which all depends upon underscore(_) in full_name.to illustrate i have full_name= 'james_9333_mathew_rohn' it means full_name has more than 1 underscore in name then i want nick_name to be ''james_rohn'.but if there is underscore at last in full_name( 'james_9333_mathew_rohn_' ) then result could be either 'james_rohn' or 'james_rohn_' both are fine for this case.when we have only 1 undersore like 'james_rohn' then nick_name should be 'james_rohn' and but if there is undersocre at last like 'james_rohn_' then result could be either either 'james_rohn' or 'james_rohn_' both are fine.please help me to solve this.


I want my final table to be


insert into test_name values( 'james_9333_mathew_rohn','james_rohn')
,('james_9333_mathew_rohn_','james_rohn_'),
('james_rohn','james_rohn'),
('james_rohn_','james_rohn_')


Post #1356986
Posted Monday, September 10, 2012 1:46 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
weston_086 (9/10/2012)
I have a table name Test_name.which has two columns full_name and nick_name.I have values only in full_name where as Nick_name column is blank which i need to update as per full_name.I have more than 1000 distinct full_name.I want to update the ccolumn according to different logic which all depends upon underscore(_) in full_name.to illustrate i have full_name= 'james_9333_mathew_rohn' it means full_name has more than 1 underscore in name then i want nick_name to be ''james_rohn'.but if there is underscore at last in full_name( 'james_9333_mathew_rohn_' ) then result could be either 'james_rohn' or 'james_rohn_' both are fine for this case.when we have only 1 undersore like 'james_rohn' then nick_name should be 'james_rohn' and but if there is undersocre at last like 'james_rohn_' then result could be either either 'james_rohn' or 'james_rohn_' both are fine.please help me to solve this.


I want my final table to be


insert into test_name values( 'james_9333_mathew_rohn','james_rohn')
,('james_9333_mathew_rohn_','james_rohn_'),
('james_rohn','james_rohn'),
('james_rohn_','james_rohn_')




You know simply repeating a vague description does nothing to help me understand it.

You can check for the existence of more than 1 underscore pretty easily.

select *, LEN(full_Name) - LEN(replace(full_name, '_', '')) as UnderscoreCount 
from test_name

This is not the most efficient code but I THINK it might be what you are looking for.

select *, LEN(full_Name) - LEN(replace(full_name, '_', '')) as UnderscoreCount
, LEFT(full_name, charindex('_', full_name)) + left(ltrim(replace(reverse(full_name), '_', ' ')), charindex(' ', ltrim(replace(reverse(full_name), '_', ' '))))
from test_name



_______________________________________________________________

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)
Post #1356988
Posted Monday, September 10, 2012 1:57 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:37 PM
Points: 4,046, Visits: 9,204
Maybe this modification to Sean's code will work better:

select *, 
LEFT(full_name, charindex('_', full_name)) + RIGHT(full_name, charindex('_', reverse(full_name),2) - 1)
from test_name




Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1356994
Posted Monday, September 10, 2012 2:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
Luis Cazares (9/10/2012)
Maybe this modification to Sean's code will work better:

select *, 
LEFT(full_name, charindex('_', full_name)) + RIGHT(full_name, charindex('_', reverse(full_name),2) - 1)
from test_name



Thanks Luis. I was a brain meltdown for some reason. I knew I was making it harder than it needed to be. Of course who knows if this is what the OP wanted.


_______________________________________________________________

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)
Post #1356996
Posted Monday, September 10, 2012 2:28 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 17, 2014 7:01 PM
Points: 91, Visits: 464
thanks for code.but it is giving me error Msg 536, Level 16, State 2, Line 3
Invalid length parameter passed to the RIGHT function.
I think it is beause in there are Full_name like 'james' means there is no underscore . and in some cases rather than name there is value like '655' .so for that cases i want 'james' and '655' only.
Post #1357010
Posted Monday, September 10, 2012 2:35 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:37 PM
Points: 4,046, Visits: 9,204
Could you correct it by yourself using a CASE statement?
Think on why would it give an "Invalid length parameter passed to the RIGHT function."
Try an post back the code to share the solution with others or check where are you having problems.



Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1357012
Posted Tuesday, September 11, 2012 2:51 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:53 PM
Points: 3,438, Visits: 5,390
Sean Lange (9/10/2012)
[quote][b]
You know simply repeating a vague description does nothing to help me understand it.


"Insanity is doing the same thing over and over again and expecting different results."
-- Albert Einstein




My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1357207
Posted Tuesday, September 11, 2012 3:20 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:09 AM
Points: 2,876, Visits: 5,201
dwain.c (9/11/2012)
Sean Lange (9/10/2012)
[quote][b]
You know simply repeating a vague description does nothing to help me understand it.


"Insanity is doing the same thing over and over again and expecting different results."
-- Albert Einstein



“Insanity is relative. It depends on who has who locked in what cage. ”
-- Ray Bradbury


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1357226
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse