1

How can I slice a string that is separated by / ?

Example:

id column1
01 x/pp/llllll
02 lll/k/uu/llll/nbbbb
03 zzzzzz/wwww/s/aa

What I want is to return the first input up to the comma:

id column1
1 x
2 lll
3 zzzzzz
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    _..return the first input until the comma:.._ There is no comma but `/`. However the much much better approach is [normalization](https://www.guru99.com/database-normalization.html) instead of storing delimited lists. – B001ᛦ Oct 14 '21 at 15:04
  • `SUBSTRING_INDEX(collumn1, '/', 1)` – Akina Oct 14 '21 at 15:06

3 Answers3

1

The below statement should do the trick!

SELECT id, SUBSTRING_INDEX(column1, '/', 1) AS column1 
FROM [TableName];
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
austdav0
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 14 '21 at 16:08
1

Welcome! As B001ᛦ said, it might not be a good idea depending on your data.

To manipulate strings, look at the String functions.

SELECT SUBSTRING_INDEX(column1, '/', 1) FROM foo;

SQL Fiddle : http://sqlfiddle.com/#!9/2ff4f7

pyb
  • 4,813
  • 2
  • 27
  • 45
-1

Use regex

SELECT
  ID,
  REGEXP_REPLACE(column1, '/.*', '')
FROM …

See live demo.

Bohemian
  • 412,405
  • 93
  • 575
  • 722