Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split dash-separated values in SQL Server?

How do I get only the middle part of the data in my table? I tried the following code, but this only removes the right part... my output should only be middle part.

For instance when I select the data 1-021514-1 the output should be 021514 without the left and right dashes

select LEFT(ticketid, CHARINDEX('-', ticketid + '-') + 4) 
from Table

My Data is:

|TicketID   |
------------
|1-021514-1 |
|10-021514-1|
|2-021514-1 |
|4-021414-1 |
like image 247
Ron Avatar asked Nov 29 '25 20:11

Ron


1 Answers

Please try:

SELECT
  LEFT(st, CHARINDEX('-', st)-1) TicketID
from
(
    SELECT
        SUBSTRING(TicketID,  CHARINDEX('-',TicketID)+1, 10000) st
    FROM Table
)x
like image 109
TechDo Avatar answered Dec 02 '25 13:12

TechDo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!