Split in SQL of a select

1

Hello friends, I have a very simple query, let's say it is

select puesto from puestos

and give me back the following

| 1 | Oficial Policiaco |

| 2 | Juez Amparo |

| 3 | etc etc |

I need to separate this select and accommodate something like this

|  p1   |  p2       |

|Oficial| Policiaco |

the data are only separated by spaces, I need to put them either in different variables or in different fields. Does anyone know how to do this? and if I can do split for spaces?

    
asked by E.Rawrdríguez.Ophanim 01.11.2018 в 23:47
source

1 answer

1

As of sql server 2016 there is the method STRING_SPLIT ()

 SELECT ProductId, Name, value  
 FROM Product  
 CROSS APPLY STRING_SPLIT(Tags, ',');  

In that example, it will be broken by commas, you would exchange it for space. Source: link

If your version of sql server is smaller, you should do something like this:

 DECLARE @CADENA VARCHAR(100) = 'Oficial Policiaco'
 SELECT CHARINDEX(' ',@CADENA)
 SELECT SUBSTRING(@CADENA,0,LEN(@CADENA) - CHARINDEX(' ',@CADENA)), 
 SUBSTRING(@CADENA,CHARINDEX(' ',@CADENA),LEN(@CADENA)-CHARINDEX(' ',@CADENA)+1)

I hope you serve

    
answered by 05.11.2018 / 10:31
source