我有一个专栏,其客户的名字和姓氏用空格分隔。我会写什么代码来解析他们两个?我试过这个,但它没有用。
SELECT LEFT (CUSTOMER_NAME, CHARINDEX(' ', CUSTOMER_NAME)) as CUST_1ST_1_NM,
substring(CUSTOMER_NAME, CHARINDEX(' ', CUSTOMER_NAME)+1, len(CUSTOMER_NAME)-(CHARINDEX(' ', CUSTOMER_NAME)-1)) as CUST_LAST_1_NM
from CUSTOMER_TABLE
谢谢!
答案 0 :(得分:2)
使用像这样的INSTR和SUBSTR是第一个让人想到的方法。
select * from test_table;
CUSTOMER_NAME
---------------
foo bar
(1 row)
select substr(CUSTOMER_NAME, 0, instr(CUSTOMER_NAME, ' ')) first_name,
substr(CUSTOMER_NAME,instr(CUSTOMER_NAME, ' ') +1) last_name
from test_table;
FIRST_NAME | LAST_NAME
------------+-----------
foo | bar
(1 row)
答案 1 :(得分:1)
您可以在Netezza中使用UDF
select SQLTOOLKIT..GET_VALUE_VARCHAR( SQLTOOLKIT..ARRAY_SPLIT(CUSTOMER_NAME,' '), 1) as first_name, SQLTOOLKIT..GET_VALUE_VARCHAR( SQLTOOLKIT..ARRAY_SPLIT(CUSTOMER_NAME,' '), 2) as last_name from test_table;
OR
select SQLTOOLKIT..REGEXP_EXTRACT(CUSTOMER_NAME, '^[^ ]*') as first_name,TRIM(SQLTOOLKIT..REGEXP_EXTRACT(CUSTOMER_NAME, ' .*$')) as last_name from test_table;