简体   繁体   English

SQL:提取最后一个字

[英]SQL: extract the last word

I have a table that looks like the following:我有一个如下所示的表:

id | cars
1  | John's Honda
2  | Andrew's red lexus
3  | James has a bmw

I need to just get the last word of the "cars" column that shows the actual "car" name I have tried the followings but I don't get the desired output我只需要得到显示实际“汽车”名称的“汽车”列的最后一个单词我已经尝试了以下但我没有得到想要的 output

select substr(cars, -1)
from t

the code above just shows me the last charater of the column.上面的代码只显示了该列的最后一个字符。 Later, I tried the following:后来,我尝试了以下方法:

select split(cars, ' ')[offset(1)] 
from t

however, I got the "Array index 1 is out of bounds (overflow)" error.但是,我收到“数组索引 1 超出范围(溢出)”错误。 Can anyone help how this can be achieved with bigquery?任何人都可以帮助如何通过 bigquery 实现这一点吗?

Consider below simple approach考虑以下简单的方法

select *, 
  array_reverse(split(cars, ' '))[offset(0)] as brand
from your_table        

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

Note: there are really many ways to accomplish your case - so anoher one would be regexp_extract(cars, r'\b(\w+)$') as brand注意:确实有很多方法可以完成您的案例 - 所以另一种方法是regexp_extract(cars, r'\b(\w+)$') as brand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM