-2

I want to extract values from a column value in string with repetitive character "-". For Ex : this is the string VAL1 = ALPHABET-ABC-PQRST-DEFGH

For the mentioned string, i am trying to get values between the "hyphens".

First column i am able to get : SUBSTRING(VAL1,0,CHARINDEX('-',VAL1) as Column1 which is ALPHABET

How can i get further values like ABC, PQRST and DEFGH. I have been trying with:

SUBSTRING(VAL1,CHARINDEX('-',VAL1)+1,PATINDEX('%-%',VAL1)) as Column2 which is not working. Kindly help.

Dipit Sethi
  • 82
  • 1
  • 7
  • 1
    What RDBMS are you *really* using? Please only tag that one. – Larnu Mar 25 '21 at 11:37
  • Thanks. i found answer from here. Solved with very easy parsename function. – Dipit Sethi Mar 25 '21 at 11:58
  • Then I'm confused as to why you marked Gordon's asnwer as the solution, if I am honest. – Larnu Mar 25 '21 at 11:59
  • Yea, might be he understood with a different perspective. And he has invested time in my question. So, let's appreciate someone's effort of doing so. Also, after his post i learnt something new like string_split function and its usage. – Dipit Sethi Mar 26 '21 at 12:16

1 Answers1

-2

Based on charindex(), I assume you are using SQL Server. You can use string_split():

select t.*, s.value
from t cross apply
     string_split(t.val1, '-') s.value;
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
  • 4
    The OP appears to want columns here, not rows. Though there are literally 1,000's of dupes for this; there is no need to answer. For example, you effectively posted this [exact answer](https://stackoverflow.com/a/66762088/2029983) not a couple of days ago. – Larnu Mar 25 '21 at 11:39
  • 4
    @Larnu But Gordon likes copy-pasting the same answers all the time (I assume he's got this big snippet box with a bunch of standard answers to customize). Why are you complaining about this particular answer when most of his rep is got this way? (Not disagreeing with you, just wondering why you brought it up here, maybe it should be on [meta]?) – Charlieface Mar 25 '21 at 14:17
  • 6
    I gave up reminding Gordon that there is a "Close as duplicate" feature a long time ago, @Charlieface . The only reason I reminded them here is because I was already commenting on the fact that i felt that this answer doesn't answer the question (and that this is probably his most common duplicate answer, at least for SQL Server based qusetions). But we're off topic for the comments here. – Larnu Mar 25 '21 at 14:21
  • @Larnu . . . I consider "Close as duplicate" to refer to the **questions** not the **answers**. I think that automated software should be providing answers if there are *similar* questions with appropriate answers. – Gordon Linoff Mar 25 '21 at 14:23
  • 18
    So point the OP, here to your "[more appropriate answer](https://stackoverflow.com/a/66762088/2029983)" @GordonLinoff by closing this question as a duplicate of that question. Putting the same answer everywhere doesn't help the community as a whole. [SO] is meant to be a repository an good answers, not a repository of answers for individuals. But I know I can never convince you of this; many others have tried. – Larnu Mar 25 '21 at 14:26
  • @Larnu . . . I consider that the job of **software**. I don't want to spend my time looking for answers. We can disagree with that. But I do close real duplicate questions, which I come across now and then. – Gordon Linoff Mar 25 '21 at 14:28
  • 27
    *"I don't want to spend my time looking for answers"* And this is where your understanding is flawed. That is ***EXACTLY*** the point of Stack Overflow, so that Users can search the internet, on their favourite search engine, and find useful and meaningful answers... As a user with 1M+ rep, you should be well aware of that. if you don't udnerstand that, I suggest asking the community about it on [meta]; I think it could be a really interesting question coming from someone like yourself. I look forward to reading it. – Larnu Mar 25 '21 at 14:30