oracle & hive- substr() & instr()

Time:2020-5-10

1. substr()

syntax:

SUBSTR (string, start_position, [length_of_substring])

eg:

substr('This is a test', 6, 2)     would return 'is' 

remarks:

  • The original string is assumed to start at position one (1).
  • If substring_length is omitted, then Oracle returns all characters to the end of char.
  • If substring_length is less than 1, then Oracle returns null.

2. instr()

syntax:

instr(string,substring,[position],[occurrence])

syntax in hive:

instr(string,substring)

eg:

select instr('corporate floor','or',3,2) from dual;  would return '14'

remarks:

  • The default values of both position and occurrence are 1, meaning Oracle begins searching at the first character of string for the first occurrence of substring.
  • The return value is relative to the beginning of string, regardless of the value of position.

My use case Oracle:

CASE UPPER(T1.COLUMN_TYPE)
    WHEN SUBSTR(RF1.PARAM_VALUE,1,INSTR(RF1.PARAM_VALUE,'~')-1) THEN SUBSTR(RF1.PARAM_VALUE,INSTR(RF1.PARAM_VALUE,'~')+1)
    ELSE T1.PRODUCT_TYPE_DESC
END AS PRODUCT_TYPE

COLUMN_TYPE: AN APPLE
PARAM_VALUE: AN APPLE~FRUIT
SUBSTR(RF1.PARAM_VALUE,1,INSTR(RF1.PARAM_VALUE,’~’)-1): AN APPLE
INSTR(RF1.PARAM_VALUE,’~’)+1: 10
This is used for field parameterization. Compare values from a corresponding field in a parameter table. If the values before ~ are consistent, the values after ~ are taken.

Recommended Today

Notes on basic learning of ruby metaprogramming

Note 1:The code contains variables, classes and methods, which are collectively referred to as language construct. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # test.rb class Greeting  def initialize(text)   @text = text  end    def welcome   @text  end end my_obj = Greeting.new(“hello”) […]