SELECT中的十进制显示,当长度来自源表时

时间:2021-07-28 17:09:13

My super simplified example would look like this:

我的超级简化示例如下所示:

create table #cucu(nr decimal(18,6), L int)
insert into #cucu(nr, L) values (12.12, 2)
insert into #cucu(nr, L) values (1.1, 3)
insert into #cucu(nr, L) values (2.2222, 4)
select nr, l/*, CONVERT(decimal(18,L), nr) AS YeahRight*/ from #cucu
drop table #cucu

I would like to add something like the commented section, which obviously does not work. (Notice the L in the comment)

我想添加一些像评论部分,这显然不起作用。 (注意评论中的L)

I need to select data from the nr column with a specific decimal length, which can be different on every row.

我需要从nr列中选择具有特定小数长度的数据,每个行的长度可以不同。

My best idea would be to add a SQL function for the YeahRight column, but because of performance, I thought I would ask if there is anything simpler than that.

我最好的想法是为YeahRight列添加一个SQL函数,但由于性能的原因,我想我会问是否还有比这更简单的东西。

1 个解决方案

#1


3  

You can use the str() function:

你可以使用str()函数:

select nr, str(nr, 18, L)

Two cautions. First, the returned value is a string. That makes sense, because you care about formatting. Second, the string will be padded with spaces, but these can be trimmed out.

两个警告。首先,返回的值是一个字符串。这是有道理的,因为你关心格式化。其次,字符串将用空格填充,但这些可以修剪掉。

#1


3  

You can use the str() function:

你可以使用str()函数:

select nr, str(nr, 18, L)

Two cautions. First, the returned value is a string. That makes sense, because you care about formatting. Second, the string will be padded with spaces, but these can be trimmed out.

两个警告。首先,返回的值是一个字符串。这是有道理的,因为你关心格式化。其次,字符串将用空格填充,但这些可以修剪掉。