gpt4 book ai didi

sql - 计算 nVarchar 的值

转载 作者:行者123 更新时间:2023-12-04 22:06:16 25 4
gpt4 key购买 nike

我有一个名为 transactionstable,其中有一个 columnrate 数据类型 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com