Teradata 15:从 STRING 中提取数字并将其放入 DECIMAL
Teradata 15: extracting number from STRING and putting it into DECIMAL
我需要像这样提取字符串:
QQ_34_5
HHR_43_9
ET_7_25
转换成 DECIMAL 数,收到这个:
34,50
43,90
7,25
正确的函数是什么?
感谢
您可以通过多种方式做到这一点。
1) 使用 substring 和 instring。
2) Strtok 函数
strtok(colname, '_', 2)||','||strtok(colname, '_', 3)||'0'
3) 正则表达式函数
使用REGEXP_REPLACE
:
SELECT
REGEXP_REPLACE(col, '[A-Za-z0-9]+_([0-9])+_([0-9])+$', ',', 1, 0) AS output
FROM yourTable;
如果你真的需要真正的小数,那么你可以转换上面的输出,例如
SELECT
CAST(REGEXP_REPLACE(col, '[A-Za-z0-9]+_([0-9])+_([0-9])+$', '.', 1, 0)
AS DECIMAL(10,2)) AS output
FROM yourTable;
另一种使用REGEXP_SUBSTR
和TO_NUMBER
的方法:
To_Number(RegExp_Substr(col, '([0-9])+_([0-9])+$'), '9999D99', 'NLS_NUMERIC_CHARACTERS = ''_.''')
我需要像这样提取字符串:
QQ_34_5
HHR_43_9
ET_7_25
转换成 DECIMAL 数,收到这个:
34,50
43,90
7,25
正确的函数是什么?
感谢
您可以通过多种方式做到这一点。
1) 使用 substring 和 instring。
2) Strtok 函数
strtok(colname, '_', 2)||','||strtok(colname, '_', 3)||'0'
3) 正则表达式函数
使用REGEXP_REPLACE
:
SELECT
REGEXP_REPLACE(col, '[A-Za-z0-9]+_([0-9])+_([0-9])+$', ',', 1, 0) AS output
FROM yourTable;
如果你真的需要真正的小数,那么你可以转换上面的输出,例如
SELECT
CAST(REGEXP_REPLACE(col, '[A-Za-z0-9]+_([0-9])+_([0-9])+$', '.', 1, 0)
AS DECIMAL(10,2)) AS output
FROM yourTable;
另一种使用REGEXP_SUBSTR
和TO_NUMBER
的方法:
To_Number(RegExp_Substr(col, '([0-9])+_([0-9])+$'), '9999D99', 'NLS_NUMERIC_CHARACTERS = ''_.''')