SQL split in one column into two columns based on delimiter(s) - sql

My data in one column(col) is as follows:
Col
Accounts::Changes
Applications::Zen::Other
Server::Access
I need this data to go to two columns. I want the first string before the delimiter (:) to go into one column (col1) and the last string after the last delimiter to go into another column (col2).
The output should be:
Col1 Col2
Accounts Changes
Applications Zen
Server Access
I am using sql server 2008 R2

You should be able to do this with basic string operations:
select left(col, charindex('::', col) - 1) as col1,
right(col, charindex('::', reverse(col)) - 1) as col2
from table t;
Here is a SQL Fiddle.

I have been able to achieve this as follows:
select A.Col1,
case when CHARINDEX('::',A.Colx)>0
then SUBSTRING(A.Colx,1,CHARINDEX('::',A.Colx)-1)
else A.Colx end Col2,
CASE WHEN CHARINDEX('::',A.Colx)>0
THEN SUBSTRING(A.Colx,CHARINDEX('::',A.Colx)+2,len(A.Colx))
ELSE NULL END as Colx3
FROM (
select
case when CHARINDEX('::',Col)>0
then SUBSTRING(Col,1,CHARINDEX('::',Col)-1)
else Col end Col1,
CASE WHEN CHARINDEX('::',Col)>0
THEN SUBSTRING(Col,CHARINDEX('::',Col)+2,len(Col))
ELSE NULL END as Colx
FROM Table1 ) as A
Although I end up getting a third column with the leftover string, I won't use it.

Related

How can I use consecutive conditional clause in SQL?

I was trying to work with CASE WHEN in Postgresql in order to evaluate something and then do another thing. However, I need not only to check two things but they must be checked in consecutive order.
Example, let's say I have 3 columns: col1, col2 and col3.
I want to check first if col1 is greater than 0. After checking this I want to check if col2 is greater than 0. If that's the case, I will create another column which will be the sum of all of them. However, I can't do this:
select case when col1>0 and col2>0 then col1+col2+col3 end as...
I need to do something like this:
select case when col1>0 then (case when col2>0 then col1+col2+col3) else NULL end as...
But that doesn't work. So, what can I do?
You were close. You can do:
select
case when col1 > 0 then case when col2 > 0 then col1 + col2 + col3 end
else NULL end as my_column1
You had missed the inner end.
By the way, a CASE expression evaluates to NULL when no when clause is matched. Therefore, else NULL is redundant.

Read multiple columns from single table based on our input string

I want to select multiple columns from single table based on my given input string in sql select statement.
Example :
If input="table_medical" i want to select the columns like medi_col1,medi_col2,medi_col2
If input="table_pharmacy" i want to select the columns like medi_phar1,medi_phar2,medi_phar1
sql("select
case when $input="table_medical" then medi_col1) //like this
please help me to complete this.
If you want this in a single query, then the same number of columns is needed -- and have compatible types.
One method uses union all:
select medi_col1, medi_col2, medi_col2
from t
where #input = 'table_medical'
union all
select medi_phar1, medi_phar2, medi_phar1
from t
where #input = 'table_pharmacy';
SET #input="table_medical";
SELECT
CASE WHEN #input="table_medical" THEN medi_col1 ELSE medi_phar1 END as medi_col1,
CASE WHEN #input="table_medical" THEN medi_col2 ELSE medi_phar2 END as medi_col2
CASE WHEN #input="table_medical" THEN medi_col3 ELSE medi_phar3 END as col3
FROM MyTable
Data types for medi_col1 and medi_phar1 needs to be same (and for the rest of columns too)

Altering the contents of a column in SQL server 2008

I exported data from an excel file. From that file, I have a column called 'ID'.For now that column has entries like the following.
ID
1.14455,
2.48755,
3.65588,
4.25415,
and so on.
I need to get rid of that comma from the column. How do I do it?
If there are no other commas in the values, then just use replace():
select replace(col, ',', '')
If there are possibly other commas:
select (case when col like '%,' then left(col, len(col) - 1)
else col
end)

pgsql parse string to get a string after certain position

I have a table column that has data like
NA_PTR_51000_LAT_CO-BOGOTA_S_A
NA_PTR_51000_LAT_COL_M_A
NA_PTR_51000_LAT_COL_S_A
NA_PTR_51000_LAT_COL_S_B
NA_PTR_51000_LAT_MX-MC_L_A
NA_PTR_51000_LAT_MX-MTY_M_A
I want to parse each column value so that I get the values in column_B. Thank you.
COLUMN_A COLUMN_B
NA_PTR_51000_LAT_CO-BOGOTA_S_A CO-BOGOTA
NA_PTR_51000_LAT_COL_M_A COL
NA_PTR_51000_LAT_COL_S_A COL
NA_PTR_51000_LAT_COL_S_B COL
NA_PTR_51000_LAT_MX-MC_L_A MX-MC
NA_PTR_51000_LAT_MX-MTY_M_A MX-MTY
I'm not sure of the Postgresql and I can't get SQL fiddle to accept the schema build...
substring and length may vary...
Select Column_A, substr(columN_A,18,length(columN_A)-17-4) from tableName
Ok how about this then:
http://sqlfiddle.com/#!15/ad0dd/56/0
Select column_A, b
from (
Select Column_A, b, row_number() OVER (ORDER BY column_A) AS k
FROM (
SELECT Column_A
, regexp_split_to_table(Column_A, '_') b
FROM test
) I
) X
Where k%7=5
Inside out:
Inner most select simply splits the data into multiple rows on _
middle select adds a row number so that we can use the use the mod operator to find all occurances of a 5th remainder.
This ASSUMES that the section of data you're after is always the 5th segment AND that there are always 7 segments...
Use regexp_matches() with a search pattern like 'NA_PTR_51000_LAT_(.+)_'
This should return everything after NA_PTR_51000_LAT_ before the next underscore, which would match the pattern you are looking for.

Removing Characters From SQL String (Search)

Should be a pretty simple one today. I have a column with a list of contracts:
As you see, some contracts have a "-...." at the end. I need to removed this and any character after this (see desired output). Unfortunately, it's not just 1 character (could be multiple/differenct #s). So I imagine it's going to be a right/len/search combo.
Thoughts?
If the contracts all have the same form, the following is almost standard SQL for doing what you want:
select (case when CTNumber like '%-%-%'
then left(CTNumber, 7)
else CTNumber
end) as DesiredOutput
from t
The like syntax is standard, the case is standard, and most databases have a left() function. This does assume that the form of the CTNumber is such that you always want the first seven characters.
You can use patindex to confirm the presence of two dashes. With a double charindex, you can then return the part of the string before the second dash:
select case
when patindex('%-%-%', col1) > 0 then
left(col1, charindex('-', col1, charindex('-', col1) + 1) - 1)
else col1
end as col1
from dbo.YourTable