Solved

Query to reorder digits of string

Posted on 2007-11-14
3
366 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.

632 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