Trying to update certain rows and its not producing the results i need

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbj2009
    New Member
    • Jul 2009
    • 5

    Trying to update certain rows and its not producing the results i need

    I am trying to update the titles of all the employees who ase listed as Sales Representative to say Sales Partners but when I execute this code it says no rows are effected and their titles are staying the same. Can someone help me figure out what I am doing wrong?

    This is what I have.

    USE Northwind
    UPDATE dbo.Employees
    SET Title = 'Sales Partners'
    WHERE Title = 'Sales Representatives '

    SELECT * FROM dbo.Employees
  • nbiswas
    New Member
    • May 2009
    • 149

    #2
    Solution to Trying to update certain rows and its not producing the results i need

    Your query is unquestionable( full correct) but I suspect that whether the exact record is matching or not(I don't have Northwind DB with me).

    However, I have prepared a sample data with a query for you. Have a look and that might help you.

    Sample data

    Code:
    declare @t table(empid int identity, empname varchar(10),title varchar(50))
    insert into @t 
    	select 'name1','Sales Representatives' union all
    	select 'name2', 'Sales             Representatives' union all
    	select 'name3', 'SalesRepresentatives' union all
    	select 'name4', '   Sales Representatives' union all
    	select 'name5' , 'some other title'
    Query

    Code:
    UPDATE @t
    SET Title = 'Sales Partners'
    WHERE REPLACE(title,' ','') like '%'+ Replace('Sales Representatives',' ','') + '%'
    select * from @t
    Output:

    Code:
    empid	empname	title
    1	name1	Sales Partners
    2	name2	Sales Partners
    3	name3	Sales Partners
    4	name4	Sales Partners
    5	name5	some other title
    As you can see, I have taken 4 variations for "Sales Representatives " and using like to find the match. Just check the same in the Northwind as how the data is stored over there!

    Hope this helps

    Comment

    Working...