使用 tsql 从文本中提取
我在 Sql 表列中有以下字符串格式
[CID]: 267 [MID]: 319A [Name]: RJR
如何在选择查询中仅提取 MID 的值(即 319A),以便我可以在连接中使用 MID。 换句话说,我需要从此文本字段中提取 MID 值,以便在联接中使用它。我复制/粘贴了该值,看起来每个值后面都有 /n (换行)字符。
提前致谢
I have the following string format in a Sql table column
[CID]: 267 [MID]: 319A [Name]: RJR
How can I extract only the value of MID which is 319A in select query so I can use the MID in a join.
In other words I need to extract the MID value from this text field to use it in a join. I copy/pasted the value and it looks like there are /n (new line) characters after each value.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以试试这个。
ltrim 和 rtrim 将修剪您的
319A
值。如果您愿意,可以在开始时尝试不使用它们。
干杯
you may try this one.
ltrim and rtrim will trim your
319A
value.you can try without them at start if you like.
Cheers
http://www.simple-talk。 com/sql/t-sql-programming/tsql-regular-expression-workbench/
向 sql server 添加正则表达式支持
Rubular 正则表达式 帮助您入门:
http://www.simple-talk.com/sql/t-sql-programming/tsql-regular-expression-workbench/
to add regex support to sql server
Rubular Regex to get you started:
一点也不干净,但如果你在 SQL 中需要它,就在这里:
使用
and
一起:
取决于发生在哪里?如果是在批处理过程中,我将使用 ID 导出这些字段,编写一个 perl one liner 来提取它们,然后将它们加载回数据库。它会比使用这些功能快得多。
如果是屏幕事件,那么我建议将它们分成 3 列,这样实际上可以节省空间。
Not clean at ALL, but if you need it in SQL, here you go:
Use
and
together:
depending where is taking place? If it is in a batch process, I would export those fields with an ID, write a perl one liner that extracts them, and then load them back to the db. it would be so much faster than using these functions.
if it is screen event, then I suggest breaking them into 3 columns instead, itll actully save you space.
不要真的认为您需要所有这些修剪和子串功能。
如果您不知道
[MID]:
和值的开头之间或值的结尾和下一个[
的开头之间可能有多少个空格,假设您要匹配的值中没有空格,您可以使用:Don't really think you need all these trimming and substring-ing functions.
If you have no idea how many spaces might be between
[MID]:
and the start of your value, or the end of your value and the start of the next[
, and assuming there are no spaces in the values you want to match, you could use: