Solved

Query to reorder digits of string

Posted on 2007-11-14
3
352 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
3 Comments
 
LVL 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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