我有一个名为 transactions
的 table
,其中有一个 column
即 rate
数据类型 nvarchar
。
列
的值rate
类似于1/2250
。
我正在寻找一个查询,如果我运行该查询,我应该得到答案 0.0004
。
类似下面的内容
Select cast(rate as int) as rate from transactions
我正在寻找的输出是 0.0004
但现在我得到的是 invalid conversion from nvarchar to int
谢谢。
最佳答案
试试这个
create table #tmpTable (rate varchar(10))
insert into #tmpTable values
('1'),('1/2250'),('1.04'),('1/4')
select case CHARINDEX('/', rate)
when 0 then cast(rate as real) --if there's no division sign, then take number as it is
else cast(SUBSTRING(rate, 1, CHARINDEX('/', rate) - 1) as real) /
cast(SUBSTRING(rate, CHARINDEX('/', rate) + 1,LEN(rate)) as real)
end
from #tmpTable
关于sql - 计算 nVarchar 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182190/