SQL 剥离文本并转换为整数
在我的数据库(SQL 2005)中,我有一个包含注释的字段,但在注释中我有一个 id,我想只删除 id,如果可能的话,将其转换为 int:
activation success of id 1010101
上面的行是 db 字段中数据的确切结构。
不,我不想在应用程序的代码中执行此操作,实际上我不想碰它,以防万一您想知道;-)
In my database (SQL 2005) I have a field which holds a comment but in the comment I have an id and I would like to strip out just the id, and IF possible convert it to an int:
activation successful of id 1010101
The line above is the exact structure of the data in the db field.
And no I don't want to do this in the code of the application, I actually don't want to touch it, just in case you were wondering ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果注释字符串与该字符串完全相同,则可以使用替换。
但几乎可以肯定不会 - 激活不成功怎么办?
你可能最终会得到嵌套的替换语句
[抱歉,无法从这个编辑屏幕判断这是否是完全有效的sql]
这开始变得混乱; 您可以考虑创建一个函数并将替换语句放入其中。
如果这是一份一次性工作,那其实并不重要。 您也可以使用正则表达式,但这非常慢(无论如何意味着您现在有两个问题)。
If the comment string is EXACTLY like that you can use replace.
It almost certainly won't be though - what about unsuccessful Activations?
You might end up with nested replace statements
[sorry can't tell from this edit screen if that's entirely valid sql]
That starts to get messy; you might consider creating a function and putting the replace statements in that.
If this is a one off job, it won't really matter. You could also use a regex, but that's quite slow (and in any case mean you now have 2 problems).
我还要补充一点,这种方法更多地基于集合,因此对于一堆数据值来说更有效。 但仅将一个值作为变量来做到这一点是非常容易的。 您可以使用像
@chvComment
这样的变量,而不是使用列注释。I would also add that this approach is more set based and hence more efficient for a bunch of data values. But it is super easy to do it just for one value as a variable. Instead of using the column comment you can use a variable like
@chvComment
.这应该可以解决问题:
根据您的示例数据,字符串中仅出现一次整数并且它位于末尾。
This should do the trick:
Based on your sample data, this that there is only one occurence of an integer in the string and that it is at the end.
我目前没有办法测试它,但是:
I don't have a means to test it at the moment, but:
您愿意编写一些代码吗? 一种选择是创建 CLR 用户定义函数,然后使用正则表达式。 您可以在此处找到更多详细信息。 这将处理复杂的字符串。
如果您上面的行始终格式为“id ####### 激活成功”,并且您的号码位于字段末尾,那么:
将为您提供:
Would you be open to writing a bit of code? One option, create a CLR User Defined function, then use Regex. You can find more details here. This will handle complex strings.
If your above line is always formatted as 'activation successful of id #######', with your number at the end of the field, then:
Will give you: