Results 1 to 7 of 7

Thread: [RESOLVED] How to swap values in SQL Server 2008!

  1. #1

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Resolved [RESOLVED] How to swap values in SQL Server 2008!

    Hi…

    I’m wondering if possible to swap values in database column using update script in sql server 2008!!
    In this case I want to update all rows in one column by a single script.
    For example if I have:

    Table name: dbo.ABC
    Column name: FGH
    Rows value:
    XXX 111 ZZZ 222
    XXX 333 ZZZ 444
    XXX 555 ZZZ 666
    XXX 777 ZZZ 888

    Rows value after swap update:
    ZZZ 222 XXX 111
    ZZZ 444 XXX 333
    ZZZ 666 XXX 555
    ZZZ 888 XXX 777

    If possible ! how the script will be?

    Regards…

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How to swap values in SQL Server 2008!

    Im not quite clear about the question. Is there one column with spaces in it or 4 separate columns? Assuming the latter it's quite easy:-
    Code:
    Update ABC
    Set Col1 = Col3,
    Col2 = Col4,
    Col3 = Col1,
    Col4 = Col2
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to swap values in SQL Server 2008!

    Quote Originally Posted by FunkyDexter
    Im not quite clear about the question. Is there one column with spaces in it or 4 separate columns? Assuming the latter it's quite easy:-
    Code:
    Update ABC
    Set Col1 = Col3,
    Col2 = Col4,
    Col3 = Col1,
    Col4 = Col2
    Thanks for reply FunkyDexter

    it's one column and the values in this column are:
    XXX 111 ZZZ 222
    XXX 333 ZZZ 444
    XXX 555 ZZZ 666
    XXX 777 ZZZ 888

    * there are spaces between the values in this column

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to swap values in SQL Server 2008!

    Code:
    update ABC
    set FGH = SUBSTRING(FGH, 9, 7) + ' ' + SUBSTRING(FGH, 1, 7)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to swap values in SQL Server 2008!

    Quote Originally Posted by Pradeep1210
    Code:
    update ABC
    set FGH = SUBSTRING(FGH, 9, 7) + ' ' + SUBSTRING(FGH, 1, 7)

    Many Thanks Pradeep1210

    It's working perfctly

    But could you please explain the script if you have a time !

    Regards...
    Last edited by HOTFIX; Nov 28th, 2008 at 07:38 AM.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to swap values in SQL Server 2008!

    The SUBSTRING function extracts the specified number of characters from the string.

    The syntax is:
    SUBSTRING ( expression, start, length)

    You could google for SUBSTRING T-SQL command to know more about this function.

    So the first substring extracts 7 characters from the field FGH starting from the 9th character. The second substring extracts 7 characters from the field FGH starting from the 1st character.

    So this makes it (7 characters starting from 9th character) + (one space) + (7 characters starting from 1st character)

    Hope you got it.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to swap values in SQL Server 2008!

    Thanks Pradeep for info about SUBSTRING function...

    Best Regrads...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width