x

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

Farzana gravatar image

Farzana
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)
WHERE
  SUBSTRING(ID,12,1)='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

Magnus Ahlkvist gravatar image

Magnus Ahlkvist
16.6k 17 20 33

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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x18

asked: Oct 16, 2012 at 05:04 AM

Seen: 577 times

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