sql server 查询字符串中的十进制格式
我在网上找到了几种解决方案,但没有一个真正能够满足我的需求。那么,这里开始了..
我的表中有几列类型为 numeric(8, 4)、numeric(8, 5) 等,当我检索数据时,它带有所有尾随 0。有没有办法可以格式化它,这样它就可以去掉所有尾随和前导的 0?我不想指定精度长度。以下是我正在寻找的一些示例:
145.5000 -> 145.5
145.6540 -> 145.654
73.4561 -> 73.4561
37.0000 -> 37
非常感谢!
I've found several solutions online, but none that really do what I'm looking for. So, here goes..
I have several columns in my table of type numeric(8, 4), numeric(8, 5), etc, and when I retrieve the data it comes with all the trailing 0's. Is there a way I can format it so it leaves off all the trailing and leading 0's? I don't want to specify a length of precision. Here's some examples of what I'm looking for:
145.5000 -> 145.5
145.6540 -> 145.654
73.4561 -> 73.4561
37.0000 -> 37
Thank you much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浮点型和实数都是近似数据类型,因此这可能不适用于您遇到的每个值。鉴于您只有 4 或 5 位精度,我认为此方法始终有效,但您需要在将其应用于生产之前对其进行很好的测试。
Float and real are both approximate data types so this may not work with every value you come across. Given that you only have 4 or 5 digits of precision, I think this method will always work, but you'll want to test it pretty well before implementing this in to production.
理想情况下,您希望在前端代码中执行此操作
,那么如果您想要的只是
37
,如何区分0.37
和37.0
呢?这是 SQL 中的一种方法,
用空格替换 0 并进行修剪,然后将空格替换回 0
示例
编辑:错过了之前的尾随点..将其添加为 case 语句
Ideally you want to do this in front-end code
so how will you distinguish between
0.37
and37.0
if all you want is37
?Here is one way in SQL
Replace 0 with space and do trim and then replace space back to 0
example
edit: missed the trailing dot before..added that as a case statement