雪花 SQL 中的子字符串替换

时间:2021-07-27 10:33:12

标签: snowflake-cloud-data-platform

我需要替换、格式化字符串以使其与键匹配。

-- 替换符号 ( ) ,不带空格, -- 用下划线替换单个空格, -- 用BD替换BAND

例如x_input = 高等教育工作者 10 级,2 级(受薪) x_output = HEW_HIGHER_EDUCATION_WORKER_LEVEL_10_BD_2_SALARIED

我用嵌套替换编写了代码,它给出了正确的输出模式

select 'Higher Education Worker Level 10, Band 2 (salaried)' as class_0,
replace(replace(replace(upper('Higher Education Worker Level 10, Band 2 (salaried)'), '(', ''), ')', ''), ' ', '_') as class_1,
replace(class_1, ',', '') as class_2,
replace(class_2, 'Band', 'BD') as class_4

有没有更优雅的方法来做到这一点,我正在阅读雪花正则表达式模式匹配帮助,但找不到更干净的方法,而且它嵌套了几次迭代。

任何提示将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

对于单个字符的替换和删除,您可以使用 export default function App = () => { const showOfferDescription = useRef(false); useEffect(() => { // Must use a useRef variable here. If I use useState, the values don't update because // the callback function just keeps a copy to the initial value and not the current value game.events.on('POINTEROVER', (id) => { showOfferDescription.current = true; }) game.events.on('POINTEROUT', (id) => { showOfferDescription.current = false; }) }, []); return ( // Passing a ref to the child doesn't cause re-render on change. I want to trigger // a re-render in Dashboard everytime I change `showOfferDescription.current` <Dashboard showOfferDescription={showOfferDescription} /> ); } ,这将大大缩短多个 translate()

查询结果与问题相同,但代码更少:

replace()

一步:

select 'Higher Education Worker Level 10, Band 2 (salaried)' as class_0,
translate(upper(class_0), ' ()', '_') as class_1,
replace(class_1, ',', '') as class_2,
replace(class_2, 'Band', 'BD') as class_4;