sql server 去除字符串最右边0 函数

-Tracy-McGrady- 2016-07-18 10:29:56
求一个函数,可以把字符串最后面的这些没用的0给他去了


1000000000000000000
1010000000000000000
1010100000000000000
1010101000000000000
1010101010000000000
1010101020000000000
1010101030000000000
1010101040000000000
1010101050000000000
1010101060000000000
1010101070000000000
1010101080000000000
1010101090000000000
1010101990000000000
1010102000000000000
1010102010000000000
1010102020000000000
1010102030000000000


结果
1
101
10101
1010101
101010101
101010102
101010103
101010104
101010105
101010106
101010107
101010108
101010109
101010199
1010102
101010201
101010202
101010203
...全文
1245 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2016-07-18
  • 打赏
  • 举报
回复
#9类型INT长度是不够,尾数为1时直接报错 1000000000000000001
kingtiy 2016-07-18
  • 打赏
  • 举报
回复

select reverse(convert(nvarchar(50),convert(int,reverse(v)))) from 
(select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000') a

--先反转,转类型,再反转就可以了.这样应该效率会高一些.
中国风 2016-07-18
  • 打赏
  • 举报
回复
如果数据如贴出来格式应该是固定的,用BIGINT完全可以实现,且效率更高 写语句: 1、应用环境 2、效率 3、简洁
LongRui888 2016-07-18
  • 打赏
  • 举报
回复
引用 3 楼 yangsh0722 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 试试这个:
select reverse(substring(reverse(v),patindex('%[1-9]%',reverse(v)),len(v)))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v
reverse(convert(bigint,reverse(v))) 这两个哪个好?[/quote] 这个要根据你自己的需求,比如你的数据位数是相对固定的,可以采用 转化为bigint的方法,如果位数不固定,可以考虑我上面的方法。
卖水果的net 版主 2016-07-18
  • 打赏
  • 举报
回复
引用 3 楼 yangsh0722 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 试试这个:
select reverse(substring(reverse(v),patindex('%[1-9]%',reverse(v)),len(v)))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v
reverse(convert(bigint,reverse(v))) 这两个哪个好?[/quote] 建议使用 1# ,如果长度发生了变化,也没什么影响;
以学习为目的 2016-07-18
  • 打赏
  • 举报
回复
引用 3 楼 yangsh0722 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 试试这个:
select reverse(substring(reverse(v),patindex('%[1-9]%',reverse(v)),len(v)))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v
reverse(convert(bigint,reverse(v))) 这两个哪个好?[/quote] everse(convert(bigint,reverse(v)))用就这个吧
中国风 2016-07-18
  • 打赏
  • 举报
回复
select REVERSE(CAST(REVERSE(v) AS BIGINT))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v
/*
1
101
10101
1010101
*/
-Tracy-McGrady- 2016-07-18
  • 打赏
  • 举报
回复
引用 1 楼 yupeigu 的回复:
试试这个:
select reverse(substring(reverse(v),patindex('%[1-9]%',reverse(v)),len(v)))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v
reverse(convert(bigint,reverse(v))) 这两个哪个好?
dongsheng10101 2016-07-18
  • 打赏
  • 举报
回复
给个例子: 方法一:(此方法前提是:你的字符串里没有“空格”字符,要不然不能使用此方法) declare @str varchar(255) set @str='1020030000' select replace( rtrim(replace(@str,'0',' ')),' ','0' ) 方法二(不受限制,通用): declare @str varchar(255),@len int set @str='1020030000' set @len=len(@str) select @len begin if substring(@str,@len,1) ='0' set @len=@len-1 else break end select left(@str,@len)
LongRui888 2016-07-18
  • 打赏
  • 举报
回复
试试这个:
select reverse(substring(reverse(v),patindex('%[1-9]%',reverse(v)),len(v)))
from 
(
select '1000000000000000000' as v union all
select '1010000000000000000' as v union all
select '1010100000000000000' as v union all
select '1010101000000000000'
)v

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧