SQL Phone Number Conversion - sql

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

Related

Convert 10JAN2000:00:00:00 to DATETIME in SQL

I have been provided with a file whereby the dates are in the aforementioned format.
I have never seen this format before so am I going to have to separate the data out and convert each section or is this actually a known format?
Cheers
Most databases have some sort of to_date() or parse_date() functionality . . . except SQL Server.
If you are using SQL Server, then this should work:
select cast(stuff('10JAN2000:00:00:00', 10, 1, ' ') as datetime)
select to_char(to_date('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss'),'yyyy-mm-dd : hh:mm:ss') from dual;
as suggested by others, knowing which database changes the answer. SQL Server isn't keen on supporting date parse function unlike oracle, please read through below threads, might help with better understanding-
Sql Server string to date conversion
to_date in SQL Server 2005
In oracle of course, we can do something like this-
select to_date('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss') from dual;
or
select to_timestamp('10JAN2000:00:00:00', 'ddmonyyyy:hh24:mi:ss') from dual;

Oracle equivalent of NVL for number datatype

What is the Oracle equivalent of NVL for number datatypes?
This works since the datatype is VARCHAR:
select nvl(enrol.OUAC_APPLNO, 'blank') Invalid_OUAC_APPLNO
But this doesn't work since the datatype is NUMBER:
select nvl(enrol.ouac_type_id, 'blank') REGTYP
There is no equivalent and no Oracle functions will accept this (apart from DECODE() but don't do that); you're going to have to convert your number to a character:
select nvl(cast(enrol.OUAC_APPLNO as varchar2(10)), 'blank')
You may need to change the number of characters you're converting to as appropriate.
However, I don't know why you're doing this at all. By definition a NULL implies non-existence. If you want to display blank in order to confer non-existence this is something that you should be doing with your presentation layer rather than the database.
it wont work because oracle wants every row should be in the same type otherwise you cannot run functions on that column, you have cast ouac_type_id to be varchar as below;
select nvl(cast(enrol.OUAC_APPLNO as varchar2(10)), 'blank') REGTYP
select nvl(TO_CHAR(enrol.OUAC_APPLNO), 'blank') REGTYP

Typecasting datetime differences

We use firebird as a local testing database and most of our clients use SQL Server or Oracle.
This works in MS SQL Server, but not in Firebird. (haven't tested it n Oracle yet)
CONVERT(char(8),MAX(p.end_Time)-MIN (p.start_Time),8) as duration
is there a way to acoomplish this same thing for (Firebird, Oracle, and MS Sql Server)?
thanks
For Firebird you need to use DATEDIFF to obtain a difference between two timestamps (datetimes). This BTW is similar to the SQL Server DATEDIFF. If you then want to cast it to char, you can use CAST, eg CAST(DATEDIFF(DAY, MAX(p.end_Time), MIN(p.start_Time)) AS CHAR(8)) should do the trick (or you can simply do CAST(MAX(p.end_Time) - MIN (p.start_Time) AS CHAR(8)) as timestamps are subtractable.
I just noticed though that the option you specify in your CONVERT will convert to hh:mi:ss. There is no such option in Firebird. If you need to convert specifically to hh:mi:ss, you might want to look at UDF libraries like FreeAdhocUDF (specifically F_SECONDS2PERIOD) or rFunc UDF. If all else fails, you could write your own UDF.
there is no CONVERT on firebird.
Use CAST:
select cast(MAX(p.end_Time) as varchar(50)) and so on...
On Oracle (Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g), you can use to_char function:
select to_char( value, [ format_mask ], [ nls_language ] ) ....
Example:
SELECT TO_CHAR ( SYSDATE - TO_DATE ( '18-10-2012', 'dd-mm-yyyy' ), '9,999.99' ) as duration FROM DUAL;
Here is the link for Oracle/PLSQL: To_Char Function

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.

Netezza SQL syntax to convert numeric YYYYMMDD format into date

We have a Netezza table that contains dates stored in a numeric YYYYMMDD format (eg 20090731).
What is the best Netezza syntax to use to convert this into date format?
eg
SELECT somefunction(20090731) as NZDATE
?
Easiest way to convert number to date would be
select date(to_char(20090731,'99999999')) as Number_As_DATE;
You can use this one as it's the best one.
SELECT TO_DATE('20090731','YYYYMMDD') as NZDATE
to_date (sk_dim_time ,'YYYYMMDD')
My efforts were thwarted originally due to invalid dates. The code bellow does work as long as you wrap it in a statement to catch bad dates.
select to_date(substring(20090731 from 1 for 8),'YYYYMMDD') as NZDATE
Obviously 20090731 should be replaced with the name of the numeric variable.
select to_date(20090731,'YYYYMMDD') as Number_As_DATE
This will work without converting to char.