Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove strings between quotes on Microsoft SQL Server 2016?

For column of string like this "This is comment 1""comment 2"3302, 0 or more comments between double quotes concatenated with an id of any length or characters of some item at the end. How to extract only the id so that the end result becomes 3302 on Microsoft SQL Server 2016?

Example Input:

Item                                                     Qty
2d3eu                                                    30
"This item is discontinued"103d2h                        20
"Just some random comment"1er3fhvd                       10
"There can be any number of comment""Like this"144       20

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Item      Qty
2d3eu     30
103d2h    20
1er3fhvd  10
144       20

>Solution :

You can extract the Item number by finding the last " in the string and then taking all the characters to the RIGHT of that. Note we add a " to the beginning of the string to ensure that there is at least one in every value:

SELECT RIGHT(Item, CHARINDEX('"', REVERSE(CONCAT('"', Item))) - 1) AS Item, Qty
FROM stock

Output:

Item    Qty
00001   30
00002   20
00003   10
00004   20

Demo on dbfiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading