简体   繁体   English

如何在 SQL 中移动小数位

[英]How to move decimal places in SQL

I want to place a decimal between the first and second digit of the interest rate and keep 2 trailing after the decimal.我想在利率的第一位和第二位之间放置一个小数,并在小数点后保留 2。 How do I go about doing this?我该怎么做呢?

SELECT TOP 10 l.PARENTACCOUNT AS [Account Number]
        , l.interestrate AS [Interest Rate]
FROM dbo.LOAN l

This is my current result:这是我目前的结果:

Account Number  Interest Rate
0000000107       9900
0000000107       11900
0000002000       5750
0000002460       10300
0000002652       9900
0000003850       0
0000004942       7510
0000004942       4990
0000004942       5000
0000006652       6790

This is my desired result:这是我想要的结果:

Account Number  Interest Rate
0000000107       9.90
0000000107       1.19
0000002000       5.75
0000002460       1.03
0000002652       9.90
0000003850       0.00
0000004942       7.51
0000004942       4.99
0000004942       5.00
0000006652       6.79

EDIT: I ended up using this to get the correct interest rate编辑:我最终使用它来获得正确的利率

ISNULL(convert(decimal(4, 2), stuff(convert(varchar(255), interestrate), 2, 0, '.')), 0)

Strange format;奇怪的格式; you cannot handle interest rates less than 1%.你不能处理低于 1% 的利率。

You can do:你可以做:

select convert(decimal(4, 2), stuff(convert(varchar(255), interestrate), 2, 0, '.'))

This converts the value to a number, adds the decimal place, and converts to a decimal.这会将值转换为数字,添加小数位,然后转换为小数。

Here is a db<>fiddle. 是一个 db<>fiddle。

Did you try to divide by 1000:您是否尝试除以 1000:

SELECT TOP 10 l.PARENTACCOUNT AS [Account Number]
       , (l.interestrate/1000) AS [Interest Rate] FROM dbo.LOAN l

Assuming interest rate is a string.假设利率是一个字符串。 You could try:你可以试试:

SELECT CONVERT(real,CONCAT(LEFT([Interest rate],1),'.', RIGHT([Interest rate],LEN([Interest rate])-1))) FROM [Table]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM