лучший способ:
создайте 4 новых столбца с крошечными или маленькими значениями типа int, а затем заполните их из вашего строкового столбца. В этот момент опустите строковый столбец. Теперь вы можете легко сделать:
UPDATE YourTable Set col4=col4+1 WHERE col4=3
Вы можете добавить вычисляемый столбец, чтобы объединить 4 значения обратно в строку, если хотите, для поддержки старого кода.
--set up existing table
create table YourTable (YourColumn varchar(20))
INSERT YourTable VALUES ('100.23.24.1')
INSERT YourTable VALUES ('100.23.24.2')
INSERT YourTable VALUES ('100.23.24.3')
INSERT YourTable VALUES ('100.23.24.9')
--add in new columns
ALTER TABLE YourTable ADD Col1 int null
ALTER TABLE YourTable ADD Col2 int null
ALTER TABLE YourTable ADD Col3 int null
ALTER TABLE YourTable ADD Col4 int null
--populate the new columns, split apart the string
UPDATE YourTable
SET Col1=PARSENAME(YourColumn,4)
,Col2=PARSENAME(YourColumn,3)
,Col3=PARSENAME(YourColumn,2)
,Col4=PARSENAME(YourColumn,1)
--remove the string column
ALTER TABLE YourTable drop column YourColumn
--add back the string column as a computed column
ALTER TABLE dbo.YourTable ADD YourColumn AS CONVERT(varchar(10),Col1)+'.'+CONVERT(varchar(10),Col2)+'.'+CONVERT(varchar(10),Col3)+'.'+CONVERT(varchar(10),Col4)
--show the table's contents
select * from YourTable
ВЫВОД:
Col1 Col2 Col3 Col4 YourColumn
----------- ----------- ----------- ----------- -------------
100 23 24 1 100.23.24.1
100 23 24 2 100.23.24.2
100 23 24 3 100.23.24.3
100 23 24 9 100.23.24.9
(4 row(s) affected)
если не считать этого, есть способ быстрого сканирования таблицы грубой силы :
UPDATE YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
пример кода:
declare @YourTable table (YourColumn varchar(20))
INSERT @YourTable VALUES ('100.23.24.1')
INSERT @YourTable VALUES ('100.23.24.2')
INSERT @YourTable VALUES ('100.23.24.3')
INSERT @YourTable VALUES ('100.23.24.9')
UPDATE @YourTable
SET YourColumn=LEFT(YourColumn,LEN(YourColumn)-2)+'.4'
WHERE LEFT(REVERSE(YourColumn),2)='3.'
select * from @YourTable
ВЫВОД:
YourColumn
--------------------
100.23.24.1
100.23.24.2
100.23.24.4
100.23.24.9