使用 tsql 从文本中提取

发布于 2025-01-08 13:49:42 字数 212 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哎呦我呸! 2025-01-15 13:49:42

你可以试试这个。

declare 

    @t varchar(100)
    set @t = '[CID]: 267 [MID]: 319A [Name]: RJR';
    select ltrim(rtrim(substring(@t,charindex('[MID]:',@t)+6,(charindex('[NAME]',@t))-(charindex('[MID]:',@t)+6))))
---------------------------------------------------------
319A

ltrim 和 rtrim 将修剪您的 319A 值。
如果您愿意,可以在开始时尝试不使用它们。


干杯

you may try this one.

declare 

    @t varchar(100)
    set @t = '[CID]: 267 [MID]: 319A [Name]: RJR';
    select ltrim(rtrim(substring(@t,charindex('[MID]:',@t)+6,(charindex('[NAME]',@t))-(charindex('[MID]:',@t)+6))))
---------------------------------------------------------
319A

ltrim and rtrim will trim your 319A value.
you can try without them at start if you like.


Cheers

我很OK 2025-01-15 13:49:42

http://www.simple-talk。 com/sql/t-sql-programming/tsql-regular-expression-workbench/

向 sql server 添加正则表达式支持

Rubular 正则表达式 帮助您入门:

\[MID\]: (.*) \[Name]:

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:

\[MID\]: (.*) \[Name]:
心碎的声音 2025-01-15 13:49:42

一点也不干净,但如果你在 SQL 中需要它,就在这里:

使用

SUBSTRING ( value_expression , start_expression , length_expression )

and

 LOCATE( string1, string2 [, start] ) 

一起:

SUBSTRING(INPUT, 
         ((SELECT LOCATE( 'MID]: ', INPUT ))+6),
         ((SELECT LOCATE( '[Name]', INPUT )) - ((SELECT LOCATE( 'MID]: ', INPUT ))+6))

取决于发生在哪里?如果是在批处理过程中,我将使用 ID 导出这些字段,编写一个 perl one liner 来提取它们,然后将它们加载回数据库。它会比使用这些功能快得多。

如果是屏幕事件,那么我建议将它们分成 3 列,这样实际上可以节省空间。

Not clean at ALL, but if you need it in SQL, here you go:

Use

SUBSTRING ( value_expression , start_expression , length_expression )

and

 LOCATE( string1, string2 [, start] ) 

together:

SUBSTRING(INPUT, 
         ((SELECT LOCATE( 'MID]: ', INPUT ))+6),
         ((SELECT LOCATE( '[Name]', INPUT )) - ((SELECT LOCATE( 'MID]: ', INPUT ))+6))

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.

笑饮青盏花 2025-01-15 13:49:42

不要真的认为您需要所有这些修剪和子串功能。

USE tempdb;
GO

CREATE TABLE #t1
(
    a INT,
    b VARCHAR(64)
);
INSERT #t1 SELECT 1, '[CID]: 267 [MID]: 319A [Name]: RJR'
UNION ALL  SELECT 2, '[CID]: 26232 [MID]: 229dd5A [Name]: RJ'
UNION ALL  SELECT 3, 'Garbage that will not match';

CREATE TABLE #t2
(
    c INT,
    d VARCHAR(32)
);
INSERT #t2 SELECT 4, '319A'
UNION ALL  SELECT 5, '229dd5A'
UNION ALL  SELECT 6, 'NO MATCH';

SELECT t1.a, t1.b, t2.c, t2.d
  FROM #t1 AS t1 
  INNER JOIN #t2 AS t2
  ON t1.b LIKE '%`[MID`]: ' + t2.d + ' %' ESCAPE '`'

GO
DROP TABLE #t1, #t2;

如果您不知道 [MID]: 和值的开头之间或值的结尾和下一个 [ 的开头之间可能有多少个空格,假设您要匹配的值中没有空格,您可以使用:

  ON REPLACE(t1.b, ' ', '') LIKE '%`[MID`]:' + t2.d + '`[%' ESCAPE '`'

Don't really think you need all these trimming and substring-ing functions.

USE tempdb;
GO

CREATE TABLE #t1
(
    a INT,
    b VARCHAR(64)
);
INSERT #t1 SELECT 1, '[CID]: 267 [MID]: 319A [Name]: RJR'
UNION ALL  SELECT 2, '[CID]: 26232 [MID]: 229dd5A [Name]: RJ'
UNION ALL  SELECT 3, 'Garbage that will not match';

CREATE TABLE #t2
(
    c INT,
    d VARCHAR(32)
);
INSERT #t2 SELECT 4, '319A'
UNION ALL  SELECT 5, '229dd5A'
UNION ALL  SELECT 6, 'NO MATCH';

SELECT t1.a, t1.b, t2.c, t2.d
  FROM #t1 AS t1 
  INNER JOIN #t2 AS t2
  ON t1.b LIKE '%`[MID`]: ' + t2.d + ' %' ESCAPE '`'

GO
DROP TABLE #t1, #t2;

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:

  ON REPLACE(t1.b, ' ', '') LIKE '%`[MID`]:' + t2.d + '`[%' ESCAPE '`'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文