I am trying to display a column that is in the VARCHAR format of 9999999999 as 9-999-99999-9.
I know that TO_CHAR and TO_NUMBER wont work and I know I'm suppose to use SUBSTR to do this.
I feel like I could get this done if I was able to use multiple SUBSTR functions together as one column like:
SELECT SUBSTR(column, 0, 1) - SUBSTR(column, 1, 3) etc. but that wont work either.
Any guidance is MUCH appreciated.
You could use a regular expression search and replace
select regexp_replace('9999999999','(.)(...)(.....)(.)','\1-\2-\3-\4') from dual;
You should be able to use a combination of the SUBSTR function and the Oracle string concatenation operator, ||.
SELECT SUBSTR(column, 1, 1)
|| '-'
|| SUBSTR(column, 2, 3)
|| '-'
|| SUBSTR(column, 4, 5)
|| '-'
|| SUBSTR(column, 9, 1)
I think that using to_char is actually the best option. It just requires a judicious use of format models.
This should do it, but allow you to extend extremely easily without having to create a new query each time.
select replace(to_char( 999999999,'9,99,99999,9'),',','-') from dual
I've created a little SQL Fiddle to demonstrate.
Just another way you can do it, by fiddling with the NLS numeric character setting:
select to_char(9999999999, 'fm9g999g99999g9', 'NLS_NUMERIC_CHARACTERS=''.-''')
from dual;
9-999-99999-9
SELECT SUBSTR(9999999999,1,1) ||
'-' ||
SUBSTR(9999999999,2,3) ||
'-' ||
SUBSTR(9999999999,4,5) ||
'-' ||
SUBSTR(9999999999,10,1) FROM DUAL;
Suppose there is Phonenumber field in address table:
The query to display the date in (999)999-9999 format is as follows:
select concat(concat('(',substr(phone,1,3),')'),substr(phone,4,3),'-',substr(phone,7,4))
as Phone from address;
Related
I have data in table 000_ABC_AXEL. The expectation is that i have to exclude data after the last '_' and get 000_ABC in oracle sql? Any suggestions?
Need sql query to achieve below
for ex:
a_222_4 -- > expected result :a_222
123_xyz_0 -- >expected result :123_xyz
A regex replacement fits your requirement nicely:
SELECT col, REGEXP_REPLACE(col, '_[^_]+$', '') AS col_out
FROM yourTable;
You can do it with simple string functions (which are much faster than regular expressions) by finding the sub-string up to the character before the last underscore:
SELECT SUBSTR(col, 1, INSTR(col, '_', -1) - 1) AS first_parts
FROM table_name;
Which, for the sample data:
CREATE TABLE table_name (col) AS
SELECT 'a_222_4' FROM DUAL UNION ALL
SELECT '123_xyz_0' FROM DUAL;
Outputs:
FIRST_PARTS
a_222
123_xyz
fiddle
I have a statement:
SELECT ('"' || TO_DATE(substr(idate,7,4) || '/' || substr(idate,3,2) || '/' || substr(idate,5,2), 'YYYY/MM/DD') || '"')
FROM heal;
which outputs
"15/02/04"
"15/01/03"
"15/01/20"
"15/01/10"
But I've created a column from this query to populate it as a DATE (ALTER TABLE heal ADD (CDATE DATE);)
While inserting
INSERT INTO heal (cdate) VALUES
('"' || TO_DATE(substr(idate,7,4) || '/' || substr(idate,3,2) || '/' || substr(idate,5,2), 'YYYY/MM/DD') || '"');
I get Error: 00984. 00000 - "column not allowed here"
When leaving outside quotes I have the same. This is something with quotes I've read in many other duplicated threads but still don't know how to quote it. Any he;p?
'idate' column as VARCHAR2(50):
b'02042015'
b'01032015'
b'01202015'
b'01102015'
I don't know why you need to make things so complicated:
UPDATE heal
SET cdate = TO_DATE(SUBSTR(idate, 3, 8), 'DDMMYYYY');
You don't need to use all that string manipulation. Just call TO_DATE once with the proper format mask.
You rather want an UPDATE, not an INSERT.
UPDATE heal
SET cdate = to_date(substr(idate, 7, 4) || '/' || substr(idate, 3, 2) || '/' || substr(idate, 5, 2), 'YYYY/MM/DD');
Here is one way to do this. I show a full demo, beginning with the creation of the table, populating the data (with NULL in the CDATE column), then the UPDATE statement. Note the use of "boilerplate text" in the format model to TO_DATE, and the use of q-quoting syntax to define strings that include single-quotes as literal characters.
In the final output, CDATE uses my current session's NLS_DATE_FORMAT parameter; the dates may look different on your system.
create table heal (idate varchar2(50), cdate date);
insert into heal (idate)
select q'[b'02042015']' from dual union all
select q'[b'01032015']' from dual union all
select q'[b'01202015']' from dual union all
select q'[b'01102015']' from dual;
select * from heal;
IDATE CDATE
------------------------------ --------------------------
b'02042015'
b'01032015'
b'01202015'
b'01102015'
update heal
set cdate = to_date(idate, q'["b'"mmddyyyy"'"]');
4 rows updated.
select * from heal;
IDATE CDATE
------------------------------ -------------------
b'02042015' 2015/02/04 00:00:00
b'01032015' 2015/01/03 00:00:00
b'01202015' 2015/01/20 00:00:00
b'01102015' 2015/01/10 00:00:00
I am trying to extract the middle of the string from record. I want to get the only middle of the string.
select instr('WUK00000105376:WUKE03960761:WUKR0093868603',':')
from dual;
I want to get only WUKE03960761 from this string.
I have tried by using sustring and instring to get the output but i am not getting.
SELECT SUBSTR(col,
INSTR(col, ':') + 1,
INSTR(col, ':', 1, 2) - INSTR(col, ':') - 1)
FROM dual
Another option is to use regexp_substring() to get the string between the two colons:
select regexp_substr('WUK00000105376:WUKE03960761:WUKR0093868603',':[A-Z0-9]+:')
from dual;
This however would also return the colons, but you can also tell regexp_substr() to return a specific group from the regex:
select regexp_substr('WUK00000105376:WUKE03960761:WUKR0093868603',':([A-Z0-9]+):',1,1,'i',1)
from dual;
But Tim's solution using substr and instr is most likely going to be a lot faster.
I have worked with MySQL and new to oracle. In MySQL we have a function SUBSTRING_INDEX(), I want to replace it in oracle, Any help plz
SQL>select SUBSTRING_INDEX('JD;EQ;0001', ';', -1) from dual;
Result:
0001
I want a same result in oracle. Is there any function in oracle that return the same result in oracle?
I have tried but no expected result.
SELECT substr('CLUBORACLE',3,2) RES FROM dual;
SELECT SUBSTR('JD;EQ;0001', INSTR('JD;EQ;0001', ';', -1) + 1) FROM dual
In the query above, INSTR('JD;EQ;0001', ';', -1) would return 6, which is the position of the last semicolon in the expression. You want to take the substring from the position after the last semicolon until the end of the string.
Look here for a good SO question about Oracle's INSTR.
You can also use regex: SELECT REGEXP_SUBSTR('JD;EQ;0001', '[^;]+', 1, your_occurance_number) FROM dual; but SUBSTR+INSTR should be faster.
I need to convert date format in ORACLE SQL Developer
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research.
Here is the command that I consultanted other people on the forum. but it throws me unrecognized command error. table name is B and column name is First
UPDATAE B
set First = concat(to_char(substring(FIRST,1,4) + '-' + substring(FIRST, 6, 2) + '-' + substring(FIRST, 9, 2) + ' ' + substring(FIRST, 12, 8));
Could anyone here help me with it? thanks in advance.
The "unrecognized command" is merely a misspelling of UPDATE:
UPDATAE B
// Should be
UPDATE B
To verify the result is what you expect before executing the UPDATE statement, use a SELECT:
SELECT
to_char(substr(FIRST,1,4) || '-' || substr(FIRST, 6, 2) || '-' || substr(FIRST, 9, 2) || ' ' || substr(FIRST, 12, 8)) AS Test
FROM B
Umm... I'm either missing something extremely obvious or everyone else is.
You want to date operations? Use to_date and to_char. I'm going to assume this ss:sss means, seconds, then fractional seconds. You date appears to be a string so we need to convert it twice:
update b
set first = to_char( to_date( my_date, 'yyyy/mm/dd-hh:mi:ss:ff3')
,'yyyy-mm-dd hh:mi:ss' )
Generally, when using dates it's far, far easier to only use date functions and the provided formats.
As an added point if you have a date, store it as a date. It'll save a world of bother later on.