3

Given this data:

Home: (708) 296-2112  

I want everything to the right of the : character.

This is what I have so far, but I'm getting no results:

right(phone1, locate(':', phone1 + ':')-1) phone

If I use left instead of right, I get just "HOME" - just for testing purposes. I know I'm close, but I'm missing something.

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Brian K
  • 87
  • 7

4 Answers4

1

You can use SUBSTRING (might be SUBSTR dependent on your version) instead:

SELECT SUBSTRING(phone1, LOCATE(':', phone1) + 1, LENGTH(phone1))
FROM yourtable
Nick
  • 138,499
  • 22
  • 57
  • 95
1

Here's a way to do it without hard-coding in Home:, so you can also use Office: or Mobile: or Fax:, or any other word followed by a colon.

This uses ADS's scripting ability to use a variable and the built-in System.iota single row table (similar to Oracle's dual). You can just use the last line, replacing test with the name of your column and system.iota with the name of your table.

declare test string;
set test = 'Home: (708) 296-2112';
select substring(test, position(':' in test) + 1, length(test)) from system.iota;
Ken White
  • 123,280
  • 14
  • 225
  • 444
1

You can use RIGHT function as follows:

RIGHT(phone1, LEN(phone1)-CHARINDEX(':', phone1))
Apoorv Mishra
  • 1,489
  • 1
  • 8
  • 12
0

You were on the right track, but your algebra is off. You want to take the full length of the string offset by the position of the colon, minus one:

right(phone1, length(phone1) - locate(':', phone1) - 1)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360