Home » SQL & PL/SQL » SQL & PL/SQL » insert a character within a string (pl sql)
insert a character within a string [message #622703] Thu, 28 August 2014 11:18 Go to next message
deahayes3
Messages: 203
Registered: May 2006
Senior Member

Hello all,

I need help creating a query that will insert a colon from the third position counting from the end of the string.
for example '1135' replace with '11:35',what function(s) should I use?

Thanks.
Re: insert a character within a string [message #622704 is a reply to message #622703] Thu, 28 August 2014 11:23 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
substr and || will do the job
Re: insert a character within a string [message #622706 is a reply to message #622704] Thu, 28 August 2014 11:33 Go to previous messageGo to next message
deahayes3
Messages: 203
Registered: May 2006
Senior Member

Thanks for reply but when I do something like this, it only returns 5 instead of 35
SELECT SUBSTR('1135', -1, 2) from dual
Re: insert a character within a string [message #622707 is a reply to message #622706] Thu, 28 August 2014 11:38 Go to previous messageGo to next message
deahayes3
Messages: 203
Registered: May 2006
Senior Member

I saw what id did wrong should have been SELECT SUBSTR('1135', -2, 2) from dual so how do I concat the 14?
Re: insert a character within a string [message #622708 is a reply to message #622707] Thu, 28 August 2014 11:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

cookiemonster wrote on Thu, 28 August 2014 18:23
substr and || will do the job


What 14?
Re: insert a character within a string [message #622709 is a reply to message #622707] Thu, 28 August 2014 11:46 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Can you copy paste your SQL*Plus session to see what you did.

And please make sure you use code tags.

[Updated on: Thu, 28 August 2014 11:48]

Report message to a moderator

Re: insert a character within a string [message #622710 is a reply to message #622708] Thu, 28 August 2014 11:49 Go to previous messageGo to next message
deahayes3
Messages: 203
Registered: May 2006
Senior Member

Im sorry, I mean '11' of the '1135'
Re: insert a character within a string [message #622711 is a reply to message #622709] Thu, 28 August 2014 11:50 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>SELECT SUBSTR('1135', -1, 2) from dual

Did you even look at the syntax for SUBSTR? Common man, you need to do some homework now, please.
Re: insert a character within a string [message #622713 is a reply to message #622703] Thu, 28 August 2014 11:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select concat(concat(to_char(trunc(to_number('1135')/100)),':'),to_char(mod(to_number('1135'),100))) res
  2  from dual
  3  /
RES
-----
11:35

Re: insert a character within a string [message #622714 is a reply to message #622703] Thu, 28 August 2014 12:22 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
deahayes3 wrote on Thu, 28 August 2014 12:18
insert a colon from the third position counting from the end of the string.


SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
  FROM  DUAL
/
Enter value for str: 1135
old   1: SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
new   1: SELECT  REGEXP_REPLACE('1135','(..$)',':\1') X

X
-----
11:35

SQL> /
Enter value for str: 345
old   1: SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
new   1: SELECT  REGEXP_REPLACE('345','(..$)',':\1') X

X
----
3:45

SQL> /
Enter value for str: 12345
old   1: SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
new   1: SELECT  REGEXP_REPLACE('12345','(..$)',':\1') X

X
------
123:45

SQL> /
Enter value for str: 12
old   1: SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
new   1: SELECT  REGEXP_REPLACE('12','(..$)',':\1') X

X
---
:12

SQL> /
Enter value for str: 1
old   1: SELECT  REGEXP_REPLACE('&str','(..$)',':\1') X
new   1: SELECT  REGEXP_REPLACE('1','(..$)',':\1') X

X
-
1

SQL> 


SY.
Re: insert a character within a string [message #622718 is a reply to message #622714] Thu, 28 August 2014 12:36 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
excellent solomon.
Re: insert a character within a string [message #622722 is a reply to message #622713] Thu, 28 August 2014 13:12 Go to previous messageGo to next message
deahayes3
Messages: 203
Registered: May 2006
Senior Member

Thanks Michel Cadot. This works perfectly.
Re: insert a character within a string [message #622730 is a reply to message #622722] Thu, 28 August 2014 13:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It was just a joke from a 4 figure string, don't take it seriously.
Will not work for any other one.

Re: insert a character within a string [message #622755 is a reply to message #622730] Fri, 29 August 2014 02:53 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The non regexp solution also requires length(and decode if you can have 1 digit numbers):
SQL> WITH DATA AS (SELECT 12345 a FROM dual
  2                UNION ALL
  3                SELECT 1234 a FROM dual
  4                UNION ALL
  5                SELECT 123 a FROM dual
  6                UNION ALL
  7                SELECT 12 a FROM dual
  8                UNION ALL
  9                SELECT 1 a FROM dual)
 10  SELECT a, SUBSTR(a, 1, LENGTH(a) - 2)||':'||SUBSTR(a,DECODE(LENGTH(a),1,-1,-2))
 11  FROM DATA;
 
         A SUBSTR(A,1,LENGTH(A)-2)||':'||
---------- --------------------------------------------------------------------------------
     12345 123:45
      1234 12:34
       123 1:23
        12 :12
         1 :1
Previous Topic: Invalid reference to variable, ORA-22806: not an object or REF
Next Topic: How to get warnings for implicit type conversions?
Goto Forum:
  


Current Time: Fri Apr 26 06:37:16 CDT 2024