MySQL在十进制之后转换子字符串丢失的数字

时间:2012-05-24 17:51:49

标签: mysql regex substring

我正在运行以下查询:

SELECT
    SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21) AS CVSS_Base_Score_Text,
    CONVERT(SUBSTRING_INDEX(SUBSTRING(pluginText,
        LOCATE('cvss', pluginText), 21),' : ',-1),UNSIGNED INTEGER)
    AS CVSS_Base_Score_Number
FROM vulnerabilities_internal
WHERE LCASE(pluginText) REGEXP '.*cvss.*'

它返回2列

CVSS_Base_Score_Text         CVSS_Base_Score Number
CVSS Base Score: 4.3         4

如何修改此查询以使CVSS_Base_Score_Number列中的数字超过小数点并显示为4.3?

解答我自己的问题:

SELECT
SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21) AS CVSS_Base_Score_Text,
CONVERT(SUBSTRING_INDEX(SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21),' : ',  
-1), 
DECIMAL(10,1)) AS CVSS_Base_Score_Number
FROM vulnerabilities_internal

2 个答案:

答案 0 :(得分:1)

以下是可以提供所需结果的查询。

SELECT
    SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21) AS CVSS_Base_Score_Text,
    CAST(SUBSTRING_INDEX(SUBSTRING(pluginText,
        LOCATE('cvss', pluginText), 21),' : ',-1) as decimal(2,1))
    AS CVSS_Base_Score_Number
FROM vulnerabilities_internal
WHERE LCASE(pluginText) REGEXP '.*cvss.*'

答案 1 :(得分:0)

SELECT 
SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21) AS CVSS_Base_Score_Text, 
CONVERT(SUBSTRING_INDEX(SUBSTRING(pluginText,LOCATE('cvss', pluginText), 21),' : ',   
-1),  
DECIMAL(10,1)) AS CVSS_Base_Score_Number 
FROM vulnerabilities_internal