def REPLACE(char, search, replacement=None): if replacement is None: replacement = NULL() if ISNULL(char) or ISNULL(search): return char char: str = extract_value(char) search: str = extract_value(search) if not ISNULL(replacement): replacement = extract_value(replacement) else: replacement = "" value = char.replace(search, replacement) return m(value)
def SUBSTR(string, position, length=None): if ISNULL(string) or ISNULL(position): return NULL() string = extract_value(string) position = extract_value(position) length = extract_value(length) if position == 0: position = 1 if position > 0: position -= 1 if length is not None: value = string[position:position+length] else: value = string[position:] return m(value)
def LOWER(string): if ISNULL(string): return NULL() string = extract_value(string) string = str(string) string = string.lower() return m(string)
def UPPER(string): if ISNULL(string): return NULL() string = extract_value(string) string = str(string) string = string.upper() return m(string)
def CHR(n): if ISNULL(n): return NULL() value = extract_value(n) if isinstance(value, str): value = int(value) value = chr(value) return m(value)
def TO_NUMBER(value): if ISNULL(value): return NULL() value = extract_value(value) value = float(value) if value.is_integer(): value = int(value) return m(value)
def padstr(to_left: bool, value, count, fill): if ISNULL(value) or ISNULL(count) or ISNULL(fill) or count <= m(0): return NULL() if fill is None: fill = m(' ') value = extract_value(value) count = extract_value(count) fill = extract_value(fill) valuelen = len(value) if count <= valuelen: value = value[:count] return value charsleft = count - valuelen filllen = len(fill) charsfill = fill * int(charsleft / filllen) charsfill += fill[:(charsleft % filllen)] if to_left: value = charsfill + value else: value = value + charsfill return value
def NVL(expr, replacement): if ISNULL(expr): return replacement return expr
def LENGTH(string): if ISNULL(string): return NULL() string = extract_value(string) return len(string)
def RTRIM(value): if ISNULL(value): return NULL() value = extract_value(value) value = value.rstrip(" ") return m(value)
def TO_CHAR(value): if ISNULL(value): return NULL() value = extract_value(value) value = str(value) return m(value)