How to split numbers and string into two columns using TSQL - sql

In my project, I have a column that contains numbers (including "-" symbol) and a string. I want to split it into two columns. The separator between numbers and string differs it can be " " or " - ". Is it possible to solve this issue by means of a TSQL query?
This TSQL engine is placed in Devexpress WinForms designer.
Example:
Col:
343234-2321 String string
402-09-12 - Another string
Just string
303-404 - Text field
Expected result
Col1
Col2
343234-2321
String string
402-09-12
Another string
NULL
Just string
303-404
Text field
Thank you in advance!

Assuming you always need to break your string in half after the end of the numeric digits as your sample data demonstrates, a possible solution is to use patindex:
with s as (
select col, PatIndex('%[A-z]%',col) d
from t
)
select col,
NullIf(Trim(case when Substring(Left(col,d-1),d-2,1)='-' then
Left(col,d-3)
else Left(col,d-1) end),'') Col1,
NullIf(Trim(NullIf(Substring(col,d,Len(col)),'')),'') Col2
from s
See Example DB Fiddle
Note, if you are using SQL2016 or prior you'll need to replace trim with nested ltrim & rtrim

Related

How to extract string between characters in Hive

I have a Hive table with a column which includes a string with multiple topic names. I am looking to split out the first topic name (and if possible the second and third). The string can contain up to 8 topic names.
The format of the string is:
["T.Topic1", "T.Topic2", "T.Topic3", "S.Topic4", "S.Topic5"]
I have tried the following but wanted to know if there was a better way that would not involve the need to remove the left characters " and the right character " in a subsequent line or a possibility to extract more than the first topic.
SELECT SUBSTR(split(l.Intent, '[\\,]')[0], 2) AS TOPIC_1
FROM Table l
Results:
"T.Topic1"
Thank you
You are really close to the solution.
I suggest that you try and tackle it in 2 stages.
Remove the Array
Split the string.
regexp_extract(l.Intent,'^\\["(.*)"\\]' ) This will get the text inside the array.
split ( text , '", "' ) will split the string into the array you want.
putting it together:
with l as (select '["T.Topic1", "T.Topic2", "T.Topic3", "S.Topic4", "S.Topic5"]' as Intent)
select
split (
regexp_extract(l.Intent,'^\\["(.*)"\\]')
, '", "' ) as array_of_topics
from l as topics;
You can now access these rows topics.array_of_topics[0],topics.array_of_topics[1],topics.array_of_topics[3]

Remove numbers from beginning of strings in a numbered list with SQLite

I have a long numbered list imported into a table, the strings are in the following format:
1. fdhsglahs sdhkgs
2. urgbvdgh ndovh
3. 8yhbnxjghr nvdfo dfhioj
...
9999. vnur neeu nu
I want to remove the numbers in the beginning of the string, the "." adjacent to the number, and any number of spaces that come immediately after the "." and before the next character (that is, before the beginning of the string itself).
Can't find a method to do that in SQLite.
Please notice, some of the strings contain numbers as part of the string, which are not to be removed.
For this requirement you can use string functions like substr(), instr() and ltrim():
select ltrim(substr(col, instr(col, '.') + 1))
from tablename
Replace col with the column's name.
this code returns the part of the string after the . left trimmed of spaces.
See the demo.
If you want to update the table:
update tablename
set col = ltrim(substr(col, instr(col, '.') + 1));
See the demo.

Oracle SQL - convert string into formula?

I would like to know if there is a way to convert a string in Oracle SQL to a formula. E.g.
I have a connect_by string with a sys_connect_by_path that has asterics instead of commas or slashes, & I want to evaluate the formula. This is:
"=1*3.4693*48*3*1"
in a specific column. Of course, each row has a different calculation.
I'm expecting to have:
SELECT function('= 1 + 1') FROM DUAL;
Depending on the content of the expression, you may be able to do it with XML / XPath
select xmlquery(replace( '1*3.4693*48*3*1', '/', ' div ' )
returning content ) .getNumberVal()
from dual;
returns 499.5792
https://www.w3schools.com/xml/xpath_operators.asp

Remove last x characters until a specific character

I got this string /uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains and I need to get just the last part of the URL (until the last /).
Then I want to replace '-' with a space. The strings are not with the same number of characters.
How can I do?
Thank you!
Solution using BigQuery functions:
select regexp_replace(last(split(x, "/")), "-", " ") from
(select
"/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains"
as x)
Here is what I tried in SQL Server
DECLARE #s VARCHAR(max)= '/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains'
SELECT REVERSE(SUBSTRING(REVERSE(#s),CHARINDEX('/',REVERSE(#s)),LEN(REVERSE(#s))))+REVERSE(REPLACE(SUBSTRING(REVERSE(#s),1,CHARINDEX('/',REVERSE(#s))-1),'-',' '))
Sorry this was for SQL Server
did you try using split in big query
SPLIT('str' [, 'delimiter']) Returns a set of substrings as a repeated string. If delimiter is specified, the SPLIT function breaks str into substrings, using delimiter as the delimiter.

How to select specific data between Quotes (")

I am reposting my question as I am new to SQL 2012.
I want to fetch the numeric data between quotes (") in the following rows,
row1:'asdalknd,"1,2,3,4",slknsdl,"5,6,7,8",snlsn'
row2:'asknd,"111,267,387,4756",snsdl,"534,646,767,348",snlssdsdsdsjkvkn'
row3'....
row4'....
row5'....
row6'...
row7'...
row8'....
The above mentioned are the rows of a single column.
I just want to extract the numerics(may be in another column for each rows)
Can anybody pls help, as this is way above my basic knowledge of t-sql.
Thanks
this is Ugly, but will eventually work:
COLUMN = 'jksjdksls#$#$##kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
left(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
instr(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
"""") -1
)
--> 123,456,789
This is what is done:
We take this string 'jksjdksls#$#$##kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
find the first occurence of " with instr(COLUMN,"""") --> returns 24
take the right end of the string with. Therefore we need to take the length of the string with len(COLUMN)--> 55 and substract the position of the first " (24)
then we need to find the second " with instr()in the right string, which we need to create again with right(COLUMN,len(COLUMN)-instr(COLUMN,"""")) and substract 1 for the ".