I can't replace a digit form a 10 or 14th digit number ?

I have a column in my database table like the column name is ID. In this column 14th digit number of students are insert. But I want to replace a digit like from '01470526100201' number I want to replace the 12th no. digit '2' into '3'.How I can do it.

more ▼

asked Oct 16, 2012 at 05:04 AM in Default

avatar image

30 2 2 2

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

SUBSTRING-function should be your friend here.

 UPDATE tbl 
   SET ID = SUBSTRING(ID,1,11) + '3' + SUBSTRING(ID,13,2)

But keep in mind that the above query won't be able to make any good use of indexes, since the WHERE-clause uses a function on the index. And since you are interested in the value of the 12th digit in a a string, no other comparison method will be able to make use of indexes either. I'm guessing your table has ID as a clustered primary key, and then the above query will result in a table scan. For a small table, that might not cause too much pain, with a large table, your server will be kept quite busy scanning the table and moving pages in and out of memory.

more ▼

answered Oct 16, 2012 at 05:38 AM

avatar image

Magnus Ahlkvist
22.5k 20 44 43

It's work.Thank you for your answer.

Oct 16, 2012 at 05:57 AM Farzana
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 16, 2012 at 05:04 AM

Seen: 1034 times

Last Updated: Oct 16, 2012 at 06:31 AM

Copyright 2018 Redgate Software. Privacy Policy