Solved

Query to reorder digits of string

Posted on 2007-11-14
3
361 Views
Last Modified: 2010-03-19
I have a nvarchar field with 13 digits. all numbers like:

1234567654321

I need to run a query that will reorganize the order of this digits.

I need the 11th digit to me moved to number 13
I need the 12th digit to me moved to number 11
I need the 13th digit to me moved to number 12

How can this be done ?   Following such example the above number would end up being:

From: 1234567654321
To: 1234567654213

Thanks

Aleks
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 20283917
to look at the results before running it:
select yourfield, left(yourfield, 10) + substring(yourfield, 12,2) + substring(yourfield, 11,1) as new_value
from yourtable

to update:

update yourtable
  set yourfield = left(yourfield, 10) + substring(yourfield, 12,2) + substring(yourfield, 11,1)
0
 
LVL 18

Assisted Solution

by:Yveau
Yveau earned 100 total points
ID: 20283936
declare @var char(13)
select  @var = '1234567654321'
select substring(@var,1,10)+substring(@var,12,2)+substring(@var,11,1)

hope this helps ...
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 300 total points
ID: 20285153

SELECT STUFF(yourField, 11, 1, '') + SUBSTRING(yourField, 11, 1)
FROM tableName

UPDATE tableName
SET yourField = STUFF(yourField, 11, 1, '') + SUBSTRING(yourField, 11, 1)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question