34,837
社区成员




if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (ID INT IDENTITY(1,1),COL VARCHAR(100))
insert into #A
select '12345.123456' union all
select '33345.123456' union all
select '45645.123456' union all
select '23445.123456' union all
select '13245.123456' union all
select '3345.123456'
select * from #A
select id , cast(col as int) col from #A
select id , cast(col as decimal(18,2)) col from #A
--float还不如使用real,
select id , cast(col as real) col from #A
select id , str(col,18,2) col from #A
create table #A (ID INT IDENTITY(1,1),COL VARCHAR(100))
insert into #A
select '12345.123456' union all
select '33345.123456' union all
select '45645.123456' union all
select '23445.123456' union all
select '13245.123456' union all
select '3345.123456'
select id , cast(col as decimal(18,6)) col from #A
drop table #A
/*
id col
----------- --------------------
1 12345.123456
2 33345.123456
3 45645.123456
4 23445.123456
5 13245.123456
6 3345.123456
(所影响的行数为 6 行)
*/
if object_id('tb') is not null drop table tb
create table tb (ID INT IDENTITY(1,1),COL VARCHAR(100))
insert into tb
select '12345.123456' union all
select '33345.123456' union all
select '45645.123456' union all
select '23445.123456' union all
select '13245.123456' union all
select '3345.123456'
select id,CAST(COL as decimal(18,6)) as col from tb
/*
id col
1 12345.123456
2 33345.123456
3 45645.123456
4 23445.123456
5 13245.123456
6 3345.123456
*/
select id,CAST(COL as float) as col from tb
/*
id col
1 12345.123456
2 33345.123456
3 45645.123456
4 23445.123456
5 13245.123456
6 3345.123456
*/
select id,cast(CAST(COL as float) as int) as col from tb
/*
id col
1 12345
2 33345
3 45645
4 23445
5 13245
6 3345
*/
if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (ID INT IDENTITY(1,1),COL VARCHAR(100))
insert into #A
select '12345.123456' union all
select '33345.123456' union all
select '45645.123456' union all
select '23445.123456' union all
select '13245.123456' union all
select '3345.123456'
select COL,CONVERT(FLOAT,COL)+1 from #A
/*
12345.123456 12346.123456
33345.123456 33346.123456
45645.123456 45646.123456
23445.123456 23446.123456
13245.123456 13246.123456
3345.123456 3346.123456
*/
select convert(decimal(18, 6),ID) as id from #a
select id , cast(col as decimal(18,6)) col from #A where
ISNUMERIC(col)=1 and (col not like '%[^0-9]%' or charindex('.',col)>1 ) and charindex('.','0'+col)<>2 and charindex(',',col)<1 and charindex('+',col)<1 and charindex('-',col)<1
create table #A (ID INT IDENTITY(1,1),COL nVARCHAR(100))
insert into #A
select '12345.123456' union all
select '33345.123456' union all
select '45645.123456' union all
select '23445.123456' union all
select '13245.123456a' union all
select '3345.123456.'
select id , cast(col as decimal(18,6)) col from #A where ISNUMERIC(col)=1
drop table #A