REGEXP_COUNT equivalent in Spark SQL - apache-spark-sql

Is there an equivalent function in Spark SQL that does what REGEXP_COUNT do as in redshift?
Basically I'm trying to count the number of substrings.
REGEXP('1->2->3->456', '->')
should return 3.

Related

max length of the string in Oracle regexp_substr and regexp_replace function

I try to find what is the maximum length of a text as input to the Oracle regexp functions.
thanks

SQL Phone Number Conversion

I have phone numbers in a SQL database stored in (xxx) xxx-xxxx format. is there any way to cast these as xxxxxxxxxx format instead?
Simply you can use REPLACE() function :
SELECT REPLACE(REPLACE(REPLACE(REPLACE('(XXX) XXX-XXXX','(',''),')',''),'-',''),' ','')
Assuming you are using Oracle or MySQL 8+, you could use REGEXP_REPLACE:
SELECT
REGEXP_REPLACE('(914) 591-8563', '\((\d{3})\)\s*(\d{3})-(\d{4})', '\1\2\3')
FROM dual;
9145918563
This solution works via regex by capturing the 10 digits, and then generating a replacement of only digits. Explore the demo below using either Oracle or MySQL 8+:
Demo

Convert number to date in oracle sql

I have different numbers looking like 40825 and I want to convert them to an actual date in Oracle SQL.
I know it should be SELECT TO_DATE(40825 ,'MM-DD-YYYY') in SQL Server, but this does not work with the same syntax in oracle SQL.
Any help?
If this number mean 4 day, 8 month, and year 2025 then, u must use to_date function with string (not nubmer) and string must looks like date mask.
SELECT TO_DATE(to_char(40825,'FM000000') ,'MMDDYY') FROM dual

split string based on character position in ORACLE 11g SQL

I'm using oracle 11g sql developer
I have a varchar2 column with dates as 0523 (mmDD).
I want to convert them to a date column and have them look like 23-05 (dd-mm)..
Any ideas?
Well, you can do string operations directly to get the format you want:
substring(c, 3, 2)||'-'||substring(c, 1, 2)
To convert to a date, you can use:
to_date('2012'||c, 'YYYYMMDD')
To convert a date back to the form you want:
to_char(<date>, 'DD-MM')

decimal formatting in sql query

I have a query as below:
Select price from myTable;
This returns value '22.50'
However, I need the format as 5.8 bytes in my program.
That is my String value should be '00022.50000000' and not '22.50'
Is there a way to achieve this directly in SQL ?
From docs, I think you want CHAR( DECIMAL( 22.50, 13, 8 ) )
I have no DB2 instance to play with so can't verify. These functions are not ANSI SQL, they are DB2 specific.