Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Example of Column I'm trying to modify:

|   Column 1                                                                        |
 asdf sixsix =IWantToReturnThisIn1Column; asdfasd seven12=IWantToReturnThisInColumn2 


I want to capture the contents between = and ; for col1a and capture everything to the right of the second = in col 2.

Rookie question, I'm sure but it's stumping me.

My desired out put would look as follows:
| Col1                      | Col2                       |
 IWantToReturnThisIn1Column | IWantToReturnThisInColumn2 | 


What I have tried:

SELECT
col1 = substring(d.column1,13,20)
Posted
Updated 31-Aug-16 3:07am
Comments
Richard Deeming 31-Aug-16 10:53am    
This is why you don't try to store multiple values in a single column. :)
Member 11820531 31-Aug-16 14:48pm    
Agreed, but not quite the answer I need :)

C#
SELECT
  SUBSTRING(col1, 14, 26) AS Col1,
  SUBSTRING(col2, 17, 26) AS Col2
FROM col


This query will bring the desired result.

Query Result:

Col1	                  Col2
IWantToReturnThisIn1Column	IWantToReturnThisInColumn2



Above can also be achieved by using CHARINDEX in case straing between "=" and ";" is required for different rows.

C#
SELECT
  SUBSTRING(Col1, (CHARINDEX('=', col1, 1) + 1), (CHARINDEX(';', col1) - (CHARINDEX('=', col1, 1) + 1))),
  SUBSTRING(Col2, (CHARINDEX('=', col2, 1) + 1), (LEN(col2) - (CHARINDEX('=', col2, 1))))
FROM col



I hope this will fulfill the requirement.
 
Share this answer
 
v3
Comments
Member 11820531 31-Aug-16 9:39am    
That works if the column is always the same length. I used one column and row as an example. But I need to split by ';' and '=' irrespective of length.
Member 11114915 - Tanvi 1-Sep-16 0:06am    
I have updated the solution and added the query to get the characters between "=" and ";" irrespective of a length. Kindly try that one.
I guess the text length is variable, so you need to build a little complicated formula.
I let you deal with details like declaring the intermediate variables or combining all this in large formulas without formulas.
SQL
SC= INSTR(d.column1, ";") -- position of ';'
EQ1= INSTR(d.column1, "=") -- position of first '='
Col1= SUBSTRING(d.column1, EQ1+1, SQ-EQ1-1)
EQ2= INSTR(d.column1, "=", SC) -- position of second '='
Col2= SUBSTRING(d.column1, EQ2+1)


Good tuto site:
SQL Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900