Skip to content Skip to sidebar Skip to footer

Extract String From Column Sql?

So I have this column called MESSAGE on my LOGS Table that has every line starting like this (well, at least they all start with the word Percentage): Percentage|15/25|1%*1569ms#0m

Solution 1:

SELECT SUBSTRING(MESSAGE,
            CHARINDEX('*',message)+1,
            CHARINDEX('#',message)-CHARINDEX('*',message)-1) as Duration 
FROM LOGS

Solution 2:

Use below mentioned SQL query to fetch the output specified by you:

SELECTDISTINCTCASEWHEN NAME ISNOTNULLAND len(NAME) >40AND (CHARINDEX('#0ms', NAME) - CHARINDEX('|1%*', NAME) -4) >0THENSUBSTRING(NAME, CHARINDEX('|1%*', NAME) +4, (CHARINDEX('#0ms', NAME) - CHARINDEX('|1%*', NAME) -4))
        ELSE''ENDFROM dbo.Table_1

We have in-build function in SQL to break the string(SUBSTRING()) or to fetch the index of any character with in a string(CHARINDEX), so by using both function we can easily find out the exact syntax as per our requirement.

Post a Comment for "Extract String From Column Sql?"