大查询中的正确案例

时间:2018-07-15 20:20:35

标签: google-bigquery

我在Big Query的“香蕉”列中有这句话“我想买香蕉”。

我想获得“我想购买香蕉”。我怎么了看到LOWER和UPPER时我期望使用PROPER(Bananas)函数,但是好像不支持PROPER大小写吗?

DZ

2 个答案:

答案 0 :(得分:4)

下面的例子是fr BigQuery Standrad SQL

#standradSQL
CREATE TEMP FUNCTION PROPER(str STRING) AS (( 
  SELECT STRING_AGG(CONCAT(UPPER(SUBSTR(w,1,1)), LOWER(SUBSTR(w,2))), ' ' ORDER BY pos) 
  FROM UNNEST(SPLIT(str, ' ')) w WITH OFFSET pos
));
WITH `project.dataset.table` AS (
  SELECT 'i Want to buy bananas' str
)
SELECT str, PROPER(str) proper_str
FROM `project.dataset.table`  

结果是

Row str                     proper_str   
1   i Want to buy bananas   I Want To Buy Bananas    

答案 1 :(得分:0)

我扩展了米哈伊尔·伯利安(Mikhail Berlyant)的答案,在大写连字符(-)之后也要大写,因为我需要对地名使用适当的大小写。必须从SPLIT功能切换为使用正则表达式来做到这一点。

我从头开始测试一个空字符串,然后返回一个空字符串(与null相对)以匹配本机UPPER和LOWER函数的行为。

CREATE TEMP FUNCTION PROPER(str STRING) AS (( 
  SELECT 
    IF(str = '', '',
      STRING_AGG(
        CONCAT(
          UPPER(SUBSTR(single_words,1,1)), 
          LOWER(SUBSTR(single_words,2))
        ), 
        '' ORDER BY position
      )
    )
  FROM UNNEST(REGEXP_EXTRACT_ALL(str, r' +|-+|.[^ -]*')) AS single_words
  WITH OFFSET AS position
));

WITH test_table AS (
  SELECT 'i Want to buy bananas' AS str
  UNION ALL
  SELECT 'neWCASTle upon-tyne' AS str
)

SELECT str, PROPER(str) AS proper_str
FROM test_table 

输出

Row str                     proper_str   
1   i Want to buy bananas   I Want To Buy Bananas  
2   neWCASTle upon-tyne     Newcastle Upon-Tyne