使用查询从变量中提取数字字符串

时间:2019-05-21 01:05:37

标签: sql google-bigquery

我在BigQuery中有一个带有变量的表,该变量带有如下所示的文本字符串:

const http = require('http');
const express = require('express');
var app = express();

... your route config

var server = http.createServer(app)
server.listen(3000, function () {
  console.log('Express is running on port 3000')
})

如何查询此表以提取变量的数字,以便获得具有以下内容的新变量:

row  path
1    /998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf
2   /ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf
3    /11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf

谢谢!

2 个答案:

答案 0 :(得分:1)

以下BigQuery标准SQL示例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '/998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf' path UNION ALL
  SELECT '/ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf' UNION ALL
  SELECT '/11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf' 
)
SELECT path, REGEXP_EXTRACT(path, r'/(\d+)/') path2
FROM `project.dataset.table`

有结果

Row path                                            path2    
1   /998777/kkjs/lksjaflkjsdf/sdfñklñ{sdf           998777   
2   /ñljasdfkgbsdf/5854654/lsjflkjsdf/asdfasdfsdf   5854654  
3   /11544/sdfsdf/asdfsdfasdfdsf/sssfdsfdsdf        11544    

答案 1 :(得分:0)

您可以使用regexp_replace()

select regexp_replace(str, '[^0-9]', '')