如何在SQL中拆分查询 [英] How to split a query in SQL
问题描述
我想使用=符号拆分查询,
输入为
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]
输出:
................................................. .....
机器名称状态日期
.......................... ............................
Testing_TestEnvironment开启2017-03-23
请改变我的疑问
我尝试了什么:
output:
......................................................
machinename status date
......................................................
Testing_TestEnvironment open 2017-03-23
Kindly alter my query
What I have tried:
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]
推荐答案
如果您有SQL Server 2016,那么有一个新的STRING_SPLIT [ ^ ]函数,但是如果您有早期版本,则在以正确的方式拆分字符串 - 或者下一个最好的方式 [ ^ ]
- 这是ve我在家里使用的版本,请参阅作者信用的代码:
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[^]
- This is the version I use at home, see the code for the author credit:
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:
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:
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
已有多种解决方案可供使用。以下是一些快速参考资料。
最简洁的方法将CSV字符串转换为TSQL中的表? - 堆栈溢出 [ ^ ]
将CSV分隔的字符串转换为SQL SERVER中的表列 [ ^ ]
请参阅将逗号分隔的字符串转换为表格:4种不同的方法--Amit的博客 [ ^ ]用于明智地调整最佳方法。
There are number of solutions already available. Below are some quick references.
Most succinct way to transform a CSV string to an table in TSQL? - Stack Overflow[^]
Convert a CSV delimited string to table column in SQL SERVER[^]
Please see Convert Comma Separated String to Table : 4 different approaches – Amit's blog[^] for adapting best approach performance wise.
这篇关于如何在SQL中拆分查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!