Patrick de Ronde
Patrick de Ronde

Reputation: 53

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.

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.

Anyone got an Idea? thanks in advance!

Upvotes: 1

Views: 906

Answers (4)

Random User
Random User

Reputation: 341

If you are using SQL Server 2016 (and onward), this seems to work :

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 :

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`;

Upvotes: 1

Eralper
Eralper

Reputation: 6612

This string to be parsed is a JSON So on SQL Server databases we can use OpenJSON table function

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

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

Output is as enter image description here

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

Upvotes: 1

Ken Lee
Ken Lee

Reputation: 8043

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

Assuming we are using Mysql, the database table is "test3" and the data field storing the datastring is "remark". In that case , the following SQL will do the job:

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"

Upvotes: 1

Marcin Rzepecki
Marcin Rzepecki

Reputation: 588

Without bothering which SQL engine you use, here is the algorithm to deal with it:

  1. Trim string with curly braces ({ and })
  2. Split string by , delimiter
  3. Search the one from splitted table which contains "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.

Upvotes: 1

Related Questions