有一個欄位 想截取里固定字串后的一個內容
比如
1.12345678id100056
2.1234id100058
3.234567id100057
需要截取每個欄位中的ID字串后的具體數字,位數固定
希望的結果是
100056
100058
100057
獲取的數字 希望和另一張的更新關聯 ,符合條件的更新數值
可以一步寫出來嗎
還是需要先存個表 在匹配更新
uj5u.com熱心網友回復:
create table #t(id int,x varchar(50))
insert into #t(id,x)
select 1,'12345678id100056' union all
select 2,'1234id100058' union all
select 3,'234567id100057'
select id,x,x2=cast(substring(x,charindex('id',x)+2,50) as int)
from #t
/*
id x x2
----------- -------------------------------------------------- -----------
1 12345678id100056 100056
2 1234id100058 100058
3 234567id100057 100057
(3 行受影響)
*/
uj5u.com熱心網友回復:
謝謝,但我的需求是能從一張表的某欄位去截取不固定的字符
然后在和其他表比較?
uj5u.com熱心網友回復:
關聯表,用right函式欄位=right (欄位1,len (欄位))
uj5u.com熱心網友回復:
測驗代碼如下;根據欄位vc更新表test02_update中id;create table test02(id int,vc varchar(200));
insert into test02
select 1,'12345678id100056'
union all
select 2,'1234id100058'
union all
select 3,'234567id100057'
create table test02_update(id_update int,vc varchar(200));
insert into test02_update
select 5,'100056'
union all
select 6,'100058'
union all
select 7,'100057'
update a set a.id_update=b.id
from test02_update as a
left join test02 as b on right(b.vc,6)=a.vc
select * from test02_update;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/22787.html
標籤:基礎類
