简体   繁体   中英

How to split a column into multiple values

table1

Value (Always 3 Digit)

100x200x300
200x400x500
...
...

I want to make 3 column from value column

Expected Output

val1 val2 val3

100 200 300
200 400 500
...
...

Need Query Help

This leverages PARSENAME rather than SUBSTRING.

SELECT
   PARSENAME(Value2, 3) AS val1,
   PARSENAME(Value2, 2) AS val2,
   PARSENAME(Value2, 1) AS val3
FROM
    (
    SELECT
        REPLACE(Value, 'x', '.') AS Value2
    FROM
        MyTable
    ) T;

This code

  • allows for different component value lengths
  • assumes x is always the separator
  • assumes always 3 components
SELECT SUBSTRING([VALUE],1,3) AS val1,
       SUBSTRING([VALUE],5,3) AS val2, 
       SUBSTRING([VALUE],9,3) AS val3
  FROM TABLE1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM