Oracle: SELECT to_number('3+11+7') FROM dual; gives ora-01722 - oracle11g

I am using oracle 11g R2. When i query SELECT to_number('3+11+7') FROM dual. It gives ora-01722.
and when i remove single quotes, SELECT to_number(3+11+7) FROM dual. Then it gives answer 21.
I just removed single quotes from string. How can i do it with built in functions.
Thanks.

You could pass evaluation of the expression to XML:
select to_number(xmlquery('3+11+7' returning content)) from dual;
db<>fiddle

Related

Teradata XMLAGG function is not working in snowflake

Select
ContractVersionId
,Origin
XMLAGG(FIRSTSTATION || '/' ORDER BY FIRSTSTATION) AS InterTransptAndCarrCode
FROM LdCONTRACTMULTISEGMENT
WHERE firstsegair1='AC' AND secondsegair1 <> 'AC'
GROUP BY 1,2,SECONDSEGAIR1
XMLAGG is a Teradata function, you can't expect all these functions to work in snowflake as-is. But you're simply trying to get a group concat and snowflake supports the Standard SQL function for it:
LISTAGG(FIRSTSTATION, '/') WITHIN GROUP (ORDER BY FIRSTSTATION)

How to replace occurrence only on the start of the string in Oracle SQL?

I have a source column and I want to search for string values starting with 05, 5 971971 and 97105 to be replaced by 9715. As showin in output table.
SOURCE OUTPUT
0514377920 971514377920
544233920 971544233920
971971511233920 971511233920
9710511233920 971511233920
I tried following which works for first case.
SELECT REGEXP_REPLACE ('0544377905', '^(\05*)', '9715')FROM dual;
But following is not working, for second case:
SELECT REGEXP_REPLACE ('544377905', '^(\5*)', '9715')FROM dual;
Something is wrong with my regular expression. As I am getting: ORA-12727: invalid back reference in regular expression.
You can provide your four patterns using alternation; that is, in parentheses with a vertical bar between them:
with t(source) as (
select '0514377920' from dual
union all select '544233920' from dual
union all select '971971511233920' from dual
union all select '9710511233920' from dual
)
SELECT source, REGEXP_REPLACE (source, '^(05|5|9719715|97105)', '9715') as output
FROM t;
SOURCE OUTPUT
--------------- --------------------
0514377920 971514377920
544233920 971544233920
971971511233920 971511233920
9710511233920 971511233920
Depending on your data and any other restrictions you have, you may be able to make it as simple as replacing the first part of any string that has a 5 in it, which works for your small sample:
SELECT source, REGEXP_REPLACE (source, '^.[^5]?5', '9715') as output
FROM t;
That matches zero or more characters that are not 5, followed by a 5. That may be too simplistic for your real situation though.

Oracle regular expression using hyphen in pattern

Why is this query returning the value 4 (I expected 0)?
select regexp_instr ('123abc','[A-Z]')
from dual;
I think [] should indicate a character list, and A-Z includes all upper letters?
This is affected by your session's NLS_SORT setting, and you will get a result of 4 if you have case-insensitive sorting enabled:
alter session set nls_sort=binary;
select regexp_instr ('123abc','[A-Z]')
from dual;
REGEXP_INSTR('123ABC','[A-Z]')
------------------------------
0
alter session set nls_sort=binary_ci;
select regexp_instr ('123abc','[A-Z]')
from dual;
REGEXP_INSTR('123ABC','[A-Z]')
------------------------------
4
You can read more in the documentation; and you may find this answer useful too.

Syntax error (missing operator) in query expression 'EXTRACT (YEAR FROM Starting_Date)'

SELECT EXTRACT (YEAR FROM Starting_Date) as Orderyear,
FROM PGME
WHERE ID =1
I tried to select the year from the Starting_Date which the format is "15/01/1968". But it keep saying the Syntax error. Any recommended? Thank you for advance.
extract is a MySQL function that isn't part of the ANSI SQL standard, and from the comments it seems you're trying to use it with MS-Access. Instead, you could consider using the datepart function which is more-or-less the MS-Access equivalent. Additionally, as lad2025 noted in his comment, you have a redundant comma after Orderyear:
SELECT DATEPART("yyyy", Starting_Date) AS orderyear
FROM pgme
WHERE id = 1

SQLite Subquery Syntax Error when using Multiple Columns

It seems so simple; but I can't get this working.
select ISIN, QuoteTimestamp
from QuoteData
where (ISIN, QuoteTimestamp) IN
(select ISIN, MAX(QuoteTimestamp) QuoteTimestamp
from QuoteData
group by ISIN)
This query gives me a syntax error. If I amend it to only use a single column (remove the MAX(QuoteTimewstamp)) it works.
It seems to be correct, as per the documentation.
Adding "AS" for the subquery alias makes no difference.
Here's the full error message from SQLite Manager.
SQLiteManager: Likely SQL syntax error: select ISIN, QuoteTimestamp
from QuoteData
where (ISIN, QuoteTimestamp) IN
(select ISIN, MAX(QuoteTimestamp) AS QuoteTimestamp
from QuoteData
group by ISIN)
[ near ",": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
This seems to work although, to me, it seems counter-intuitive. It is returning "Symbol", "Bid" and "Ask" from the row with MAX(QuoteTimestamp).
select ISIN, Symbol, Bid, Ask, MAX(QuoteTimestamp)
from QuoteData
group by ISIN
I'm sure Oracle and their ilk would tell me that Symbol, Bid and Ask are not group-by expressions.
Thanks for all the help.
Thanks,
Al.
In SQLite 3.7.11 or later, you do not need to use a subquery at all:
select ISIN, MAX(QuoteTimestamp) QuoteTimestamp
from QuoteData
group by ISIN

Resources