这个语句当列中的字符为Asss_050时,会出现转换错误,谢谢了。 SQL code: if not object_id('tb') is null drop table tb Go Create table tb([列1] nvarchar(9)) Insert tb select N'B978_722' union all select N'A9786_721' union all select N'C80516_6' union all select N'Asss_050' Go select 列1=cast(right(列1,len(列1)-charindex('_',列1))as int) from tb /* 列1 ----------- 722 721 6 50
(4 個資料列受到影響) */
SQL code:
select cast(right(col1,charindex('_',reverse(col1))-1) as int) from (values('B978_722'), ('A9786_721'), ('C80516_6'), ('Asss_050') ) tb(col1)