weird update statement

  • 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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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_')

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.
    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
  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.

  • 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.
    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
  • Sean Lange (9/10/2012)


    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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (9/11/2012)


    Sean Lange (9/10/2012)


    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!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (9/11/2012)


    dwain.c (9/11/2012)


    Sean Lange (9/10/2012)


    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

    Darnit, Eugene, quit talking about my relatives!

    Weston_086, I agree that Luis's code is probably the best choice, but I think you are making things extremely difficult on yourself by doing the "either James_Rohn or James_Rohn_ is acceptable". Not knowing your business rules or the reason why that last underscore would or should matter, makes it extremely difficult for us to give you the assistance you truly deserve.

    Can you explain (in non-tech speak) why this table exists, who will be consuming the nickname data, why you might have names like 'james_9333_mathew_rohn' and why that second or third underscore are so important to the end users?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply