简体   繁体   English

如何在 SQL 中选择字符串的一部分

[英]how do I select a part of a string in SQL

So I got a long string with data about a product and I only need the EAN code.所以我得到了一个关于产品数据的长字符串,我只需要 EAN 代码。

the datastring looks like this:数据串如下所示:

{"ArtNr UP":"782001","Omschrijving":"1-2DRY OKSELPADS LARGE WIT","Inhoud":" 20ST","Bruto Inkoop":"2,36","Advies":"4,09","Discount":"4,09","EAN-code CE":"8717775824442","Merk\r":"1-2DRY\r"}

I only need the number that comes after EAN-code CE":", I thought about using SUBSTRING but that wouldn't work because all the entries are different lenghts.我只需要 EAN 代码 CE":" 后面的数字,我想过使用 SUBSTRING 但这不起作用,因为所有条目的长度都不同。

Anyone got an Idea?有人有想法吗? thanks in advance!提前致谢!

Without bothering which SQL engine you use, here is the algorithm to deal with it:不用担心您使用哪个 SQL 引擎,这里是处理它的算法:

  1. Trim string with curly braces ( { and } )用花括号( {} )修剪字符串
  2. Split string by , delimiter分割字符串,分隔符
  3. Search the one from splitted table which contains "EAN-Code CE"从包含“EAN-Code CE”的拆分表中搜索一个
  4. Split that part(if exists) by : delimiter分割该部分(如果存在) :分隔符
  5. Get second part(if exists) and trim with " .获取第二部分(如果存在)并使用"修剪。

At a result you should receive EAN code value.结果,您应该收到 EAN 代码值。

Since EAN location number has a fixed length of 13 digits, we just need the SQL to locate the place where EAN starts and then display the appropriate data由于EAN位置号有13位的固定长度,我们只需要SQL定位EAN开始的地方,然后显示相应的数据即可

Assuming we are using Mysql, the database table is "test3" and the data field storing the datastring is "remark".假设我们使用的是Mysql,数据库表是“test3”,存储数据串的数据字段是“remark”。 In that case , the following SQL will do the job:在这种情况下,以下 SQL 将完成这项工作:

select 
SUBSTRING(remark, locate('EAN-code CE', remark)+14, 13) from test3; 

note: if you are using MSSQL, then please use "CHARINDEX" instead of "locate"注意:如果您使用的是 MSSQL,那么请使用“CHARINDEX”而不是“locate”

This string to be parsed is a JSON So on SQL Server databases we can use OpenJSON table function这个要解析的字符串是一个 JSON 所以在 SQL Server 数据库上我们可以使用 OpenJSON 表函数

Here is sample code for a variable string这是变量字符串的示例代码

declare @str nvarchar(max) = '{"ArtNr UP":"782001","Omschrijving":"1-2DRY OKSELPADS LARGE WIT","Inhoud":" 20ST","Bruto Inkoop":"2,36","Advies":"4,09","Discount":"4,09","EAN-code CE":"8717775824442","Merk\r":"1-2DRY\r"}'

select * from openJSON(@str) where "key" = 'EAN-code CE'

If you store this JSON string in a database table then the code can be used as follows如果将此 JSON 字符串存储在数据库表中,则可以使用如下代码

select * 
from TableName
cross apply openJSON(str) with (
    "EAN-code CE" varchar(20)
)

In this case str is the column including JSON data in TableName table在这种情况下, str 是 TableName 表中包含 JSON 数据的列

Output is as输出为在此处输入图片说明

You will find OpenJSON samples at Parse JSON using SQL OpenJSON which will be useful if you require more than one column in return list您将使用 SQL OpenJSONParse JSON 中找到 OpenJSON 示例,如果您需要返回列表中的多列,这将非常有用

If you are using SQL Server 2016 (and onward), this seems to work :如果您使用的是 SQL Server 2016(及更高版本),这似乎有效:

CREATE TABLE TEST(
DUMMY NVARCHAR(MAX));
INSERT INTO TEST(DUMMY) VALUES('{"ArtNr UP":"782001","Omschrijving":"1-2DRY OKSELPADS LARGE WIT","Inhoud":" 20ST","Bruto Inkoop":"2,36","Advies":"4,09","Discount":"4,09","EAN-code CE":"8717775824442","Merk\r":"1-2DRY\r"}');

SELECT JSON_VALUE(DUMMY,'$."EAN-code CE"') as EANCode
FROM TEST

Edit : Since you are using MySQL, the equivalent appears to be the ->> operator :编辑:由于您使用的是 MySQL,因此等效项似乎是 ->> 运算符:

CREATE TABLE `JSON_TABLE` (
  `JSON` TEXT DEFAULT NULL
);

INSERT INTO `JSON_TABLE` VALUES ('{"ArtNr UP":"782001","Omschrijving":"1-2DRY OKSELPADS LARGE WIT","Inhoud":" 20ST","Bruto Inkoop":"2,36","Advies":"4,09","Discount":"4,09","EAN-code CE":"8717775824442","Merk\\r":"1-2DRY\\r"}');

SELECT JSON->>'$."EAN-code CE"' FROM `JSON_TABLE`;

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

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