简体   繁体   English

根据SQL Server中的条件从定界字符串中获取子字符串

[英]Taking Substring from a delimited string based on condition in SQL Server

I have a delimited 我有定界

string ";2;5;2;5;04/02/2014;3;100.000000;5;04/02/2015;3;100.000000;5;04/02/2016;3;100.000000;5;04/02/2017;3;100.000000;5;04/02/2018;3;100.000000;"

The format for the data is as follows: 数据格式如下:

1st character:  Delimiter used within this format field
Number of dimensions 
Delimiter
Number of rows
Delimiter
Number of columns
Delimiter
...Data Elements…
Delimiter

I want to extract this data inside a query or a function to get the Data Elements in a table format like 我想在查询或函数中提取此数据以获取表格式的数据元素,例如

ScheduleDate    ScheduleValue
04/02/2015  100.000000
04/02/2016  100.000000
04/02/2017  100.000000
04/02/2018  100.000000
04/02/2016  34428.9700
04/02/2017  34428.9700
04/02/2018  34428.9700
04/02/2019  34428.9700

Also here “5” indicates a date, “04/02/2015” is the actual date, “3” indicates a price and “100.00000” is the actual price. 另外, “5”表示日期, “04/02/2015”是实际日期, “3”表示价格, “100.00000”是实际价格。 Similarly other indicators are like : 同样,其他指标如下:

       1 Character
       2 Numeric
       3 Price  
       4 Security
       5 Date    
       6 Time    
       7 Date or time      
       8 Bulk
       9 Month/Year  

I need to configure in that function or query to accommodate this as well. 我需要在该功能或查询中进行配置以适应此要求。 I would be highly obliged if some one can help. 如果有人可以帮助我,我将非常有义务。

to get rid of the first three parameters one of the things you can do this: 要摆脱前三个参数,您可以执行以下操作之一:

DECLARE @string VARCHAR(max)
SET @string=';2;5;2;5;04/02/2014;3;100.000000;5;04/02/2015;3;100.000000;5;04/02/2016;3;100.000000;5;04/02/2017;3;100.000000;5;04/02/2018;3;100.000000;'
DECLARE @newstring VARCHAR(MAX)
SET @newstring=CAST((SELECT CAST('<Node>'+REPLACE(@string,';',';</Node><Node>')+'</Node>' AS XML).query('//Node[position()>4]/text()') ) AS VARCHAR(MAX))
SELECT @newstring

so this will basically leaves you data type and data, which you can parse with the existing code you mentioned 因此,这基本上将剩下您的数据类型和数据,您可以使用您提到的现有代码进行解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM