Redshift 中的英国邮政编码区域
UK Postcode area in Redshift
我正在尝试使用 SQL (Redshift) 提取英国邮政编码的第一部分
例如,BT28 8YT 应该 return BT - 所以基本上是第一个或两个字母字符,但它不应该 return 一个数字。我发现英国政府的正则表达式有点难用!
这是我目前所拥有的,但我正在努力解决我应该使用的正则表达式函数,所以 redshift 会抛出语法错误。
[Amazon]() Invalid operation: syntax error at or near "REGEXP";
SELECT
IF((left(orders.customer_postcode,2) REGEXP '[0-9]') = 0, left(orders.customer_postcode,2), left(orders.customer_postcode,1)) AS "orders.postcode_area"
FROM orders
邮政编码区域始终是一两个字母后跟一个数字。
看起来你可以使用 REGEXP_SUBSTR 来做到这一点:
http://docs.aws.amazon.com/redshift/latest/dg/REGEXP_SUBSTR.html
select regexp_substr(customer_postcode,'^[a-zA-Z]{1,2}') from orders
您也可以使用 SIMILAR TO 来执行此操作。例如,对于 WC 邮政编码,您可以使用:
postcode similar to 'WC[1-2][A-Z][[:space:]][A-Z0-9]{3}'
等
我正在尝试使用 SQL (Redshift) 提取英国邮政编码的第一部分 例如,BT28 8YT 应该 return BT - 所以基本上是第一个或两个字母字符,但它不应该 return 一个数字。我发现英国政府的正则表达式有点难用!
这是我目前所拥有的,但我正在努力解决我应该使用的正则表达式函数,所以 redshift 会抛出语法错误。
[Amazon]() Invalid operation: syntax error at or near "REGEXP";
SELECT
IF((left(orders.customer_postcode,2) REGEXP '[0-9]') = 0, left(orders.customer_postcode,2), left(orders.customer_postcode,1)) AS "orders.postcode_area"
FROM orders
邮政编码区域始终是一两个字母后跟一个数字。
看起来你可以使用 REGEXP_SUBSTR 来做到这一点:
http://docs.aws.amazon.com/redshift/latest/dg/REGEXP_SUBSTR.html
select regexp_substr(customer_postcode,'^[a-zA-Z]{1,2}') from orders
您也可以使用 SIMILAR TO 来执行此操作。例如,对于 WC 邮政编码,您可以使用:
postcode similar to 'WC[1-2][A-Z][[:space:]][A-Z0-9]{3}'
等