how to update column city value from 'A' to 'B' and 'B' to 'A' in single query

  • hi,

    I have a table like

    id city

    1 A

    so i want to update city column from A to B and again B to A by in single statement..please help.

    Thanks

    Dastagiri

  • UPDATE myTable

    SET City = CASE WHEN City = A THEN B

    WHEN City = B THEN A

    END

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • hi,

    The below query only updating the city value from A to B not B to A

    Please advice.

    Thanks,

    Dastagiri

  • Koen's query will work because SQL does updates in two phases, first where it reads the data second where it changes.

    If it's not doing what you want, please post actual data and desired results (and the table's definition) so we can see what's happening.

    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
  • hi,

    one of the interviewer asked the question about updating...so i have added test table like below...

    USE [Practice]

    GO

    /****** Object: Table [dbo].[mytable] Script Date: 09/25/2013 13:10:59 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[mytable](

    [city] [nchar](10) NULL

    ) ON [PRIMARY]

    GO

    and i have inserted values like

    insert into mytable (city) values ('A')

    so now i want to write update querty to update city value from A to B then B to A by using any query....

  • Ah ok, so you want to update a value to a new value and then back to it's old value.

    It seems rather pointless, are you sure that's what the interviewer meant?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen,

    Yes , i agree with you but i don't know its possible..i was not replied any thing when he raised that question becuase i thought that by using any recursive cte it will work.. So i searching for that..

  • So update it, then update it back?

    Update MyTable

    SET City = 'B'

    WHERE City = 'A';

    Update MyTable

    SET City = 'A'

    WHERE City = 'B';

    You can't do it in one statement, because updating a column to one value and then updating it to another require two statements. Though you could just have a single statement that does nothing and it'll have the same effect.

    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
  • hi,

    any single statement to complete this task

  • Sure.

    SELECT 'Nothing' FROM myTable;

    Et voila, the row still has the value A.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • dastagiri16 (9/25/2013)


    hi,

    any single statement to complete this task

    Um...

    GilaMonster (9/25/2013)


    You can't do it in one statement, because updating a column to one value and then updating it to another require two statements. Though you could just have a single statement that does nothing and it'll have the same effect.

    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
  • Koen Verbeeck (9/25/2013)


    Sure.

    SELECT 'Nothing' FROM myTable;

    Et voila, the row still has the value A.

    I can simplify that...

    ;

    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
  • DECLARE @t TABLE(city VARCHAR(1))

    INSERT INTO @t(city)

    VALUES ('A'),( 'B'),( 'C'),( 'A'),( 'B'),( 'C'),( 'A');

    -- do a select and not the results for comparison purposes

    SELECT city FROM @t

    UPDATE @t

    SET city = CASE city

    WHEN 'A' THEN 'B'

    WHEN 'B' THEN 'A'

    else city

    END

    -- select after update to compare with initial select

    select * from @t

    SQL 2000/2005/2008/2012 DBA - MCTS/MCITP

  • I'll get into the spirit of this thing you've got going here!

    CREATE TABLE #MyTable

    (

    city nchar(10)

    );

    INSERT INTO #MyTable VALUES ('A');

    WITH SampleData (city) AS

    (

    SELECT 'A' UNION ALL SELECT 'B' UNION ALL SELECT 'A'

    )

    UPDATE a

    SET city = b.city

    FROM #MyTable a

    JOIN SampleData b ON 1=1;

    SELECT * FROM #MyTable;

    GO

    DROP TABLE #MyTable;

    😛


    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

  • hi,

    here i am not able to trust it is updated with 'A'

    supose in cte last select statement can be changed to 'AA' it is not showing last updated record that is 'AA'

    Thanks,

    Dastagiri

Viewing 15 posts - 1 through 15 (of 36 total)

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