Oracle SQL - convert string into formula? - sql

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

Related

How to split numbers and string into two columns using TSQL

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

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.

How to return string within quotes

I'm querying a dataset using Oracle SQL Developer and want to create a column with partial return data from a (CLOB) in another column.
The part I need is in quotes and i've used substring to extract however the value will change as will the length of the string. Is there a way to end when reach closing quotes instead of specifying length of string?
dbms_lob.substr(a.LINETESTRESULT,15,dbms_lob.instr(UPPER(a.LINETESTRESULT),'LHCRAG')+11) AS REFRESH_RAG
At the minute, 15 characters are returned, but the latest additions are shorter and showing the ", from the next entry. I need to replace the length attribute.
The result I get is:
Red_Session",
I need the output to be: Red_Session
or whatever the return value is, I just need it to end before the closing quote.
INSTR has a parameter nth (Occurrence number, starting at 1.). With this you can create something like this.
dbms_lob.substr(a.LINETESTRESULT,dbms_lob.instr(a.LINETESTRESULT, '"', 1, 2) - dbms_lob.instr(a.LINETESTRESULT, '"') - 1, dbms_lob.instr(a.LINETESTRESULT, '"') + 1) AS REFRESH_RAG
you might try REGEXP_SUBSTR like below. Alternatively you can find the position of the quote and use the substr...
SELECT
REGEXP_SUBSTR(a.LINETESTRESULT,
'"([^"]*)') AS REFRESH_RAG
FROM DUAL;
Managed to get this using the following code:
SELECT
dbms_lob.substr(UPPER(a.LINETESTRESULT), dbms_lob.instr(UPPER(a.LINETESTRESULT), '"',
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11) -
(dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11),
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11)
AS REFRESH_RAG

replace all occurrences of a sub string between 2 charcters using sql

Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.

SQL query search within a string

I have a column of data in my database that has a project number the numbers are formatted like this, YYYYnnnn.ee (for example 20140124.00). Since there are three distinct parts of the number, the user could search by either the YYYY or the nnnn. I have written a query that searches by the YYYY. I need to write a query for the nnnn.
How would you write a query to search for a specific string in a specific location of another string?
You can use SUBSTRING for that.
The first paramter to SUBSTRING should be the columname, the next parameter is the index of the start character and the last parameter is the number of characters.
The example below will return the nnnn part from your column. In the example below we are searching for records where the nnnn equals 1234.
SELECT *
FROM tablename
WHERE
(SUBSTRING(columnName, 5, 4) = '1234')
Please note, if your column is a number (decimal). You will need to first cast it as a varchar (string). For example:
SELECT *
FROM tablename
WHERE
(SUBSTRING(CAST(columnName AS VARCHAR(20)), 5, 4) = '1234')