varchar 到数字的转换
我有一个问题困扰着我。当 varchar 值由字母组成时,如何将 varchar 转换为数字。
对于我的 varchar 价格列值:
14dollars10cents
15dollars20cents
通过将其转换为 varchar 到数字价格列,值应该是:
1410
1520
我知道如果 varchar 不包含任何字母,它可以通过“自动转换”
SELECT CONVERT(INT, PRICE) FROM Table
有没有办法摆脱字母在中间,因为我想对其进行数学函数
的更新尝试:
SELECT CAST (Replace(REPLACE(PRICE, 'dollars', '.'),'cents','') AS Number(4,2)))
FROM TEST;
谢谢。
I have a question that bothers me. How can i convert a varchar to number when inside the varchar value consists of alphabets.
For my varchar price column values:
14dollars10cents
15dollars20cents
By converting it to varchar to number price column, the values should be:
1410
1520
I know that if the varchar does not consists any alphabets, it can auto convert by"
SELECT CONVERT(INT, PRICE) FROM Table
Is there any way to get rid of the alphabets in the middle as I would like to do mathematical function on it.
Updated attempt of putting fixed point number in:
SELECT CAST (Replace(REPLACE(PRICE, 'dollars', '.'),'cents','') AS Number(4,2)))
FROM TEST;
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
REGEXP_REPLACE
删除所有非数字字符:然后将其转换为数字:
如果您想添加点,那么您也可以使用
REGEXP_REPLACE
来做到这一点:瞧...
You could just use
REGEXP_REPLACE
to remove all non digit characters:To then convert this to a number:
If you want to add the point in then you can do that with
REGEXP_REPLACE
too:Voila...
问题是如果 varchar 仍然包含字母数字字符,它就会中断。
The issue with this is it will break if the varchar still contains alpha-numeric characters.
如何使用
translate
去除不需要的字符。How about using
translate
to strip out the unwanted characters.不,我不认为有直接的方法。
您可以进行字符串解析来获取整数值。
No I don't think there is direct way.
you can do string parsing to get your integer value.