Sql server 2008 强制转换(带小数的子字符串)
我有一行代码,可以将我的列转换成一个数字Sql server 2008 强制转换(带小数的子字符串),sql-server-2008,casting,numbers,substring,decimal,Sql Server 2008,Casting,Numbers,Substring,Decimal,我有一行代码,可以将我的列转换成一个数字 case when Auth_Amt LIKE '%DAY%' then cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as numeric) when Auth_Amt LIKE '%TAX%' then cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as
case when Auth_Amt LIKE '%DAY%' then cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as numeric) when Auth_Amt LIKE '%TAX%' then cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as numeric)
when Auth_Amt LIKE '%SCHG%' then cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as numeric)
else 0
end as Amt_Day
我可以添加什么来让它返回两个小数点。现在是整数返回。您可以这样尝试:
case when Auth_Amt LIKE '%DAY%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%TAX%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%SCHG%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
else 0
end as Amt_Day
CAST(yourField as decimal(13,2))
一般情况下,您必须按如下方式进行铸造:
case when Auth_Amt LIKE '%DAY%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%TAX%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%SCHG%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
else 0
end as Amt_Day
CAST(yourField as decimal(13,2))
您可以这样尝试:
case when Auth_Amt LIKE '%DAY%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%TAX%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%SCHG%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
else 0
end as Amt_Day
CAST(yourField as decimal(13,2))
一般情况下,您必须按如下方式进行铸造:
case when Auth_Amt LIKE '%DAY%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION('/DAY' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%TAX%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
when Auth_Amt LIKE '%SCHG%' then CAST(cast(SUBSTRING(Auth_Amt FROM 1 FOR POSITION(' ' IN Auth_Amt) - 1) as decimal(13,2)) as decimal(13,2))
else 0
end as Amt_Day
CAST(yourField as decimal(13,2))
我要把它放在这行下面吗?当像“%TAX%”这样的Auth_Amt时,然后将(子字符串(Auth_Amt FROM 1表示位置(“”在Auth_Amt)-1中)转换为数字)@Matt:-你可以尝试像
一样转换(将(子字符串(Auth_Amt FROM 1表示位置(“”在Auth_Amt)-1中)转换为数字)为十进制(13,2))
Aswesome。这种方法很有效。现在发生的是,我的行从字符串的第一部分提取了一个数字。示例26.50。我的行返回了27。(将26.50从文本转换为数字)但是当我把它拿回来的时候,它把它四舍五入到了27。用你的代码,它把它带回来了27.00。有没有更好的方法来做我所说的,这样它会把它带回来26.50。看起来它是四舍五入,然后我把它变成了小数,所以我失去了变化?@Matt:-当你把它转换成数字的时候,那么可能是在做四舍五入。因此,我认为您需要在那里进行更改。让我编辑我的回答。我是否将其放在这行下?当验证金额如“%TAX%”时,然后将其转换为十进制(子字符串(验证金额从1开始表示位置(“”在验证金额中)-1)作为数字)@Matt:-您可以尝试将其转换为十进制(子字符串(验证金额从1开始表示位置(“”在验证金额中)-1)作为数字)Aswesome。这种方法很有效。现在发生的是,我的行从字符串的第一部分提取了一个数字。示例26.50。我的行返回了27。(将26.50从文本转换为数字)但是当我把它拿回来的时候,它把它四舍五入到了27。用你的代码,它把它带回来了27.00。有没有更好的方法来做我所说的,这样它会把它带回来26.50。看起来它是四舍五入,然后我把它变成了小数,所以我失去了变化?@Matt:-当你把它转换成数字的时候,那么可能是在做四舍五入。所以我认为你需要在那里做些改变。让我编辑我的答案