How to split string based on column length and insert into table - sql

I have a string that I need to split and create table from it.
00001 00000009716496000000000331001700000115200000000000
I know the exact length of each column:
Col1 = 5
Col2 = 7
Col3 = 23
etc...
I need something like this (Empty values are NULL's)
Can you direct me to the right way of doing that?

Use substring():
select substring(col, 1, 5) as col1,
substring(col, 6, 2) as col2,
. . .

you can use computed column to improve your performance(visit https://www.sqlservertutorial.net/sql-server-basics/sql-server-computed-columns/)
use below function to fill your column
SUBSTRING(string, start, length)

Related

Splitting the string into columns to extract values using BigQuery

How can i split the string by a specific character and extract the value of each. The idea is that i need to extract each word between the line including the start/end of the string as this information represents something. Is there a regex pattern ? or a way to split the info into columns ?
Name
A|B|C|D|E|F|G
Name col1 col2 col3 col4 col5 col6 col7
A|B|C|D|E|F|G A B C D E F G
I am using BigQuery for this and couldn't find a way to get the info of all of those. I tried the regex code which only works for the case where we have A|B|C.
I have to compare each column value and then create conditions using case when
CODE:
select
regexp_extract(name, "\\w+\\S(x|y)") as c2, -- gives either x or y
left(regexp_substr(name, "\\w+\\S\\w+\\S\\w+"),1) as c1,
right(regexp_extract(name, "\\w+\\S\\w+\\S\\w+"),1) as c3
from Table
Consider below approach
select * from (
select *
from your_table, unnest(split(name, '|')) value with offset
)
pivot(any_value(value) as col for offset in (0,1,2,3,4,5,6))
if applied to dummy data as in your question - output is
This seems like a use case for SPLIT().
select split(name,"|")[safe_offset(0)] as c1, split(name,"|")[safe_offset(1)] as c2, ..
from table
see https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
Added use of safe_offset instead of offset per Array index 74 is out of bounds (overflow) google big query

Oracle - Divide a string based on total length of string

I have a non standardized data in one of the column and I need to be splitting the data into 3 parts.
there is no specific requirement on length each column should have. It's just that the entire data to be split into 3
Input Data 01 : test , test ,test/test
Input data 02: Test; test,test\testing123data123datadatatawerr
OutPut 01: Col1=test Col2= test,test Col3=/test
Output 02: Col1= Test; col2= test,test col3=\testing123data123datadatatawerr
Is there a way to take the total length and based on that divide into 3 parts.
Concatenating of split data I need to get the entire string back.
I need to be splitting the data into 3 parts. there is no specific requirement on length each column should have
The simplest approach is to use substr():
select
substr(col, 1, 1) col1,
substr(col, 2, 1) col2
substr(col, 3) col3
from mytable
The first two columns contain one character each, starting from the beginning of the string; the last column contains the reminder. This guarantees that each column will be fed (provided that the string is at least 3 characters).
On the other hand if you want to split in three parts whose length is close to equal, you can do:
select
substr(col, 1, round(length(col) / 3)) col1,
substr(col, 1 + round(length(col) / 3), round(length(col) / 3)) col2
substr(col, 1 + 2 * round(length(col) / 3)) col3
from mytable

oracle substr from character until character

I have a string with multiple / characters. I need to trim the string before/after each of that character. Example string "231/19/2812-27/1". Each trim will be its own column.
Row1 | Row2 | Row3 | Row4 | Row5
231 | 19 | 2812 | 27 | 1
Please no regex.
Thank you.
Edit: Edited how the string should be trimmed and represented as a table (27/1)
try this
select SUBSTR('231/19/2812-27/1',1,instr('231/19/2812-27/1','/',1)-1) col1,
SUBSTR('231/19/2812-27/1',instr('231/19/2812-27/1','/',1)+1,(instr('231/19/2812-27/1','/',1,2)-instr('231/19/2812-27/1','/',1))-1) col2 ,
SUBSTR('231/19/2812-27/1',instr('231/19/2812-27/1','/',1,2)+1,(instr('231/19/2812-27/1','-',1,1)-instr('231/19/2812-27/1','/',1,2))-1) col3 ,
SUBSTR('231/19/2812-27/1',(instr('231/19/2812-27/1','-',1,1)+1)) col4
from dual;
First of all, I'd suggest representing the result in rows, not columns, because in the latter case you have to know the number of columns to construct a query or use dynamic SQL which is quite more complicated and absolutely redundant in your case.
Try this:
with t as (select '231/19/2812-27/1' as str from dual)
select regexp_substr(str,'[^/]+',1,level) as val
from t
connect by regexp_substr(str,'[^/]+',1,level) is not null
Yes, it's a regexp and, in my opinion, the easiest way to achieve the result you want. If you do not want to use this then write your own function to split a string.
But, if you still want to represent your result in columns, try this:
--edit removed a , at the end
SELECT REGEXP_SUBSTR(t.value, '[^/]+', 1, 1) col1,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 2) col2,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 3) col3,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 4) col4
FROM (select '231/19/2812-27/1' as value from dual) t;

SQL Strings - Filter by Hypen(x number)

I am trying to formulate a query that will allow me to find all records from a single column with 3 hyphens. An example of a record would be like XXXX-RP-XXXAS1-P.
I need to be able to sort through 1000s of records with either 2 or 3 hyphens.
You can REPLACE the hyphens in the string with an empty string and compute the difference of the length of original string and the replaced string to check for the number of hyphens.
select *
from yourtable
where len(column_name)-len(replace(column_name,'-',''))=3
and substring(column_name,9,1) not like '%[0-9]%'
If your records have 2 or 3 hyphens, then just do:
where col like '%-%-%-%'
This will get 3 or more hyphens. For exactly 3:
where col like '%-%-%-%' and col not like '%-%-%-%-%'
try this,
declare #t table(col1 varchar(50))
insert into #t values ('A-B'),('A-B-C-D-E'),('A-B-C-D')
select * from
(SELECT *
,(len(col1) - len(replace(col1, '-', ''))
/ len('-')) col2
FROM #T)t4
where col2=3

Remove values that net each other out to zero, leaving only one positive 2472320 in this case

492953BI -2284424
492953BI -2014941
492953BI -1916038
492953BI -1908036
I need to split the first coloumn (ID) into two coloumns. (ie. Have the numbers in first coloumn, and BI in the second.) Am battling to do this on SQL Server. Am also new to SQL so battling to work with older questions, thank you
Using Left() and Right() functions:
--If the number part is always 6 digits
Select left(yourColumn,6) Col1, right(yourColumn,2) Col2
From yourTable
--For any number of front digits (bit more generic)
Select left(yourCol, charIndex('B',yourCol)-1) Col1,
right(yourCol, len(yourCol) - charIndex('B',yourCol)+ 1) Col2
From yourTable
select substring(col1, 1, 6) as new_col1,
substring(col1, 7, 2) as new_col2
from your_table
select substring(ID, 1, 6) as ID,
substring(ID, 7, 2) as SubID from Table
this may give result as in two column ID and SubID.
thanks