简体   繁体   English

使用 SQL 查找 Substring

[英]Find Substring Using SQL

I have a column log .我有一个专栏日志 I need to extract values from that log column and make 4 new columns我需要从该日志列中提取值并创建4 个新列

My table:我的桌子: 在此处输入图像描述

Output I need: Output 我需要:

在此处输入图像描述

If the string really always follows exactly that format, you can use something funky like this:如果字符串真的总是完全遵循这种格式,你可以使用如下时髦的东西:

DECLARE @test NVARCHAR(1000)
    = N'Super admin James Johnson<jamesjohnson@gmail.com> updated profile of admin Sam Welson<samwelson@gmail.com>';

SELECT *
,      PARSENAME(repl.val, 4) AS AdminName
,      PARSENAME(repl.val, 3) AS AdminEmail
,      PARSENAME(repl.val, 2) AS UpdateName
,      PARSENAME(repl.val, 1) AS UpdateEmail
FROM (
         VALUES (@test)
     ) AS X (T)
OUTER APPLY (
                VALUES (REPLACE(
                                   REPLACE(
                                              REPLACE(
                                                         REPLACE(X.T, 'Super admin ', '') --Remove this completely
                                                     ,   ' updated profile of admin ' --Replace this with a .
                                                     ,   '.'
                                                     )
                                          ,   '<'   --Replace this with a . and [ (so the . in the email isn't parsed)
                                          ,   '.['
                                          )
                               ,   '>' --Reaplace with ] to complemanted the previous ]
                               ,   ']'
                               )
                       )
            ) AS repl (val);

Which gives:这使:

+------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------+---------------+------------------------+------------+---------------------+
|                                                     T                                                      |                                   val                                   |   AdminName   |       AdminEmail       | UpdateName |     UpdateEmail     |
+------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------+---------------+------------------------+------------+---------------------+
| Super admin James Johnson<jamesjohnson@gmail.com> updated profile of admin Sam Welson<samwelson@gmail.com> | James Johnson.[jamesjohnson@gmail.com].Sam Welson.[samwelson@gmail.com] | James Johnson | jamesjohnson@gmail.com | Sam Welson | samwelson@gmail.com |
+------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------+---------------+------------------------+------------+---------------------+

This abuses the PARSENAME function, which is meant to parse 4 part named objects (server_name.database_name.schema_name.object_name).这滥用了PARSENAME function,它旨在解析 4 部分命名对象 (server_name.database_name.schema_name.object_name)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM