简体   繁体   中英

Regex to find a pattern using sql

I have a column called 'user_details' in a SQL table 'customers' with the below value in it:

{4:"2021-06-07T16:17:26.327+02:00",5:"1623075805735.phna3uyo",6:"www.abc.com/connexion",10:"loggedOut",12:"567879",2:"1026530505.1619610156",3:"event"}
{4:"2021-06-01T13:11:34.742+02:00",5:"1622545894742.ml2cyuw",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"",2:"435305774.1622545085",3:"event"}
{4:"2021-06-01T10:13:30.85+02:00",5:"1622535210085.vlowlxmj",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"278356",2:"1381684281.1622534907",3:"event"}
{4:"2021-06-01T10:24:51.808+02:00",5:"1622536405142.h45exkgg",6:"seigneuriegauthier.com/connexion",10:"loggedOut",12:"251666",2:"1019448131.1621925108",3:"event"}
{4:"2021-06-01T14:13:54.476+02:00",5:"1622551449049.k14838ij",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"601322",2:"1975087820.1622548509",3:"event"}

I'm trying to extract the id after number 12:" without the "" ie 567879,278356 etc into another column.

Since there are many reputative "" I'm unable to build the regex expression. I tried the below but didn't get the exact match

(?<=:)"[0-9][0-9][0-9][0-9][0-9][0-9]

How can I write a SQL query to retrieve this. Pls help.

On SQL Server, with no regex, support, we can try using the base string functions SUBSTRING along with CHARINDEX :

SELECT
    user_details,
    SUBSTRING(user_details,
              CHARINDEX('12:"', user_details) + 4,
              CHARINDEX('"', user_details, CHARINDEX('12:"', user_details) + 4) -
                  CHARINDEX('12:"', user_details) - 4) AS val
FROM customers;

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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