在SQL语句里面如何将字符型转换成数字型?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在SQL语句里面如何将字符型转换成数字型?相关的知识,希望对你有一定的参考价值。
参考技术A先检查金额列的数据是否都符合小数规范,用cast(金额 as decimal(10,4))>500或者
convert(decimal(10,4),金额)>500
注:decimal的长度和小数位数根据自己需要设定
sql中将字符串数字转换成数字:
方法一:SELECT CAST('123' AS SIGNED integer);
方法二:SELECT CONVERT('123',SIGNED);
方法三:SELECT '123'+0;
扩展资料:
能将字符型转换成数字型,就能将数字转化为字符,对于精确数值的数据类型,转换出来的字符串就是我们存储的数值。比如:
declare @i int
set @i=123456789
print 'test:'+convert(varchar(20),@i)
输出就是:test:123456789
而如果对于近似数值的数据类型,那么可就没有那么简单了。
declare @i float
set @i=123456789
print 'test:'+convert(varchar(20),@i)
输出结果:test:1.23457e+008
在SQL语句里面如何将字符型转换成数字型
在SQL语句里面如何将字符型转换成数字型
sql字符串转数字
用cast(金额 as decimal(10,4))或者 convert(decimal(10,4),金额)
SELECT 2023, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A]-- where SalaryMonth like '2015.%'
union all
SELECT 2015, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2015.%'
union all
SELECT 2016, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2016.%'
union all
SELECT 2017, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2017.%'
union all
SELECT 2018, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2018.%'
union all
SELECT 2019, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2019.%'
union all
SELECT 2020, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2020.%'
union all
SELECT 2021, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2021.%'
union all
SELECT 2022, sum( convert(decimal(10,4),[paymoney])) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2022.%'
union all
SELECT SalaryMonth, convert(decimal(10,4),[paymoney]) FROM [IMS_PUB_1].[dbo].[A] where SalaryMonth like '2022.%'
(无列名) (无列名)
2023 887096.4100
2015 73909.5800
2016 86034.6400
2017 106196.8500
2018 111773.7400
2019 126841.2600
2020 108108.4000
2021 151869.9500
2022 122361.9900
2022.01 12426.3800
2022.02 12426.3700
2022.03 12426.3800
2022.04 12426.3700
2022.05 5794.2000
2022.06 9263.3100
2022.07 11555.1100
2022.08 10029.8400
2022.09 12004.6800
2022.1 12004.6700
2022.11 12004.6800
以上是关于在SQL语句里面如何将字符型转换成数字型?的主要内容,如果未能解决你的问题,请参考以下文章
怎么把“文件路径”这个字符串转换成数字型?转换完成后还是文件路径吗?