There is no redshift inbuilt function to strip a character from start-end of a string.
For eg. if a numeric value is stored as a character with preceding zeros ' 0000123' and you want to store/operate/aggregate/join it as a number it is not possible with inbuilt redshift functions. What you can do is to create the following strip udfs and make a use of these.
lstrip : strips out the left instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_lstrip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.lstrip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('00001234','0') would result in 12345
rstrip : strips out the right instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_rstrip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.rstrip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('12340000','0') would result in 12345
strip: strips out the left & right instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_strip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.strip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('000012340000','0') would result in 12345
For eg. if a numeric value is stored as a character with preceding zeros ' 0000123' and you want to store/operate/aggregate/join it as a number it is not possible with inbuilt redshift functions. What you can do is to create the following strip udfs and make a use of these.
lstrip : strips out the left instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_lstrip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.lstrip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('00001234','0') would result in 12345
rstrip : strips out the right instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_rstrip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.rstrip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('12340000','0') would result in 12345
strip: strips out the left & right instances of a character from a string.
CREATE OR REPLACE FUNCTION public.fn_strip(str_in character varying, a character)
RETURNS character varying AS
' try:
return(str_in.strip(a))
except:
return None'
LANGUAGE plpythonu VOLATILE;
eg. select public.fn_lstrip('000012340000','0') would result in 12345
Comments
Post a Comment