Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to split a query using "=" symbol,

Input is "
Testing_TestEnvironment=open=2017-03-23
"

I have tried to get output, But is there any other way to achieve this.

SELECT  Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 1)) As [machinename], Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 2)) As [status] , Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 3)) As [date] 
 FROM  (Select  filter_date from tbl_AlertNotify where alert_title = 'Size Monitor') As [duptable] 


output:
......................................................
machinename status date
......................................................
Testing_TestEnvironment open 2017-03-23



Kindly alter my query

What I have tried:

SQL
SELECT  Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 1)) As [machinename], Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 2)) As [status] , Reverse(ParseName(Replace(Reverse(filter_date), '=','.'), 3)) As [date] 
 FROM  (Select  filter_date from tbl_AlertNotify where alert_title = 'Size Monitor') As [duptable] 
Posted
Updated 20-Mar-17 23:49pm
v3

If you have SQL Server 2016 then there is a new STRING_SPLIT[^] function, but if you have an earlier version there is a full discussion of various methods at Split strings the right way - or the next best way[^]

[EDIT] - This is the version I use at home, see the code for the author credit:
SQL
CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
	-- a comment in the source
	DECLARE @source varchar(max) = 'http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/'
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
        
    END 
    RETURN 
END
Which you would call like this:
SQL
select * from dbo.fnSplitString('Testing_TestEnvironment=open=2017-03-23','=')

Or if you want the results formatted as per your expected output then call it like this:
SQL
SELECT [1] AS [machinename], [2] AS [status], [3] AS [date]
FROM (
    select ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS X, * from dbo.fnSplitString('Testing_TestEnvironment=open=2017-03-23','=')
) as qry
PIVOT
(
    MAX(splitdata)
    FOR [X] IN ([1],[2],[3])
)AS pvt
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 21-Mar-17 10:20am    
5
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900