2015-09-23 111 views
3

我有一個客戶的名和姓用空格分隔的列。我會寫什麼代碼來解析他們兩個?我試過這個,但它不起作用。拆分姓氏和名字Netezza

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 

謝謝!

回答

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) 
+0

使用的UDF這樣的幫助偉大的工作,謝謝! – Drew

1

你可以在Netezza公司

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;