不使用存储过程在dash上拆分字符串 [英] Split string on dash without using stored procedure

查看:210
本文介绍了不使用存储过程在dash上拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个,但是它有效,但它不是动态的,它需要是每个分隔符( - )将在不同的位置。



使用database;

选择substring(comment,0,5)作为col1,

substring(comment,6,5)as col2,

substring(comment,12,5)as col3
来自tillsummaries的


其中datalength(评论)> 0



通过使用子串函数,它将允许我选择我想要的信息列,告诉它起始位置和长度,但我不是确定如何使用变量。



我认为我需要这样做的方式(请告诉我,如果我完全错了)我需要3不同的变量。



@start

@length

@separator



@start = 0



然后SQL需要计算到下一个@seperator才能找到长度。

然后设置@length。



一旦完成,我认为声明应该是这样的



选择子字符串(评论,@ start,@ length)



但是这就是我失去它并感到困惑的地方.net我只需要输入一个if语句来遍历每一行,直到分隔符,将var放在一个数组中并将其分配给一个表或那种性质的东西。



如果我使用上面的select语句示例,我不知道如何调整col2和col3 @start和@length属性,因为会有多行。

I've written this, and it works, but it's not dynamic, which it needs to be as the delimiter (-) will be in different positions each time.

use "database";
select substring(comment, 0, 5) as col1,
substring(comment, 6, 5) as col2,
substring(comment, 12, 5) as col3
from tillsummaries
where datalength(comment) > 0

By using the substring function, it will allow me to select the column of info I want, tell it the start position, and length, but I'm not sure on how to use variables.

The way I think I need to do this (and please tell me if I'm totally wrong) is I need to have 3 different variables.

@start
@length
@separator

@start = 0

SQL would then need to count to the next @seperator to find the length.
This would then set @length.

Once it's done that I think the statement should go something like this

select substring(comment, @start, @length)

But that's where I lose it and get confused, as generally with .net I would simply type an if statement to run through each line until the delimiter, place that var in an array and assign that to a table or something of that nature.

If I use the above select statement example, I have no idea how I could adjust the col2 and col3 @start and @length properties as there would be multiple rows.

推荐答案

使用@charindex。就像@charindex(评论,' - ')。
Use @charindex. Like @charindex(comment,'-').


我知道你不想使用一个程序,但在我看来,使用一个函数可以使这更容易。使用函数的好处是你不需要在语句中添加过多的逻辑,并且可以在任何地方重复使用相同的代码块。



如果有兴趣,有很多例子如何构建分割函数。例如,一种简单但有效的方式来分割使用Transact-SQL的字符串 [ ^ ]或http://sqlmag.com/site-files/sqlmag.com/files/archive/sqlmag.com/content/content/21071/listing_01.txt [ ^ ]
I understand that you don't want to use a procedure but in my opinion using a function would make this much more easier. The advantages of using a function is that you don't need to add excessive logic to the statement and the same block of code can be reused everywhere.

If interested, there are a lot of examples how to build a split function. For example An Easy But Effective Way to Split a String using Transact-SQL[^] or http://sqlmag.com/site-files/sqlmag.com/files/archive/sqlmag.com/content/content/21071/listing_01.txt[^]


如果您知道列结果中只有两个分隔符。

您可以结合使用TSQL字符串函数来获得所需的结果。



这里有一些SQL可以完成这项工作。

If you know only two delimiters will be within your column result.
You could use TSQL string functions in conjunction to get your desired result.

Here is a bit of SQL that will do the job.
with TabDelimOfValue as (
--setup dummy data
	select '123-456-789' val
	union select '12-345-6789'
	union select '1-234-56789'
	union select '1-2345-6789'
	union select '1-23456-789'
	union select '1-234567-89'
	union select '1-2345678-9'
)
select 
	charindex('-', val) delimPos1,
	len(val) - charindex('-', reverse(val)) + 1 delimPos2,
	substring(val, 0, charindex('-', val)) Sub1,
	substring(val, (charindex('-', val) + 1), (len(val) - charindex('-', reverse(val)) - charindex('-', val))) Sub2,
	substring(val, len(val) - charindex('-', reverse(val)) + 2, (charindex('-', reverse(val)) - 1)) Sub3,
	val
from TabDelimOfValue;



尽管可以通过这种方式完成,但很难理解语句中发生的事情并且难以维护。



如果可能的话,我建议使用解决方案2中指定的用户定义函数。



我在前几天阅读了一篇文章如果我找到了你需要的udf,我会把它添加到这里。



希望有所帮助。



编辑

找到链接 - 好像你可能已经读过了已经是一个,这是你的其他一个问题的解决方案。

但这里是:

http://social.technet.microsoft.com/wiki/contents/articles/26937。 t-sql-splitting-a-string-into-multiple-columns.aspx [ ^ ]


这篇关于不使用存储过程在dash上拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆