简体   繁体   English

SQL解析NVARCHAR字段

[英]SQL Parse NVARCHAR Field

I am loading data from Excels into database on SQL Server 2008. There is one column which is in nvarchar data type. 我正在将数据从Excel加载到SQL Server 2008上的数据库中nvarchar数据类型中有一列。 This field contains the data as 此字段包含的数据为

Text text text text text    text text text text text.
(ABC-2010-4091, ABC-2011-0586,     ABC-2011-0587, ABC-2011-0604)     
Text text text text text    text text text text text. 
(ABC-2011-0562,     ABC-2011-0570, ABC-2011-0575, ABC-2011-0588)    

so its text with many sentences of this kind. 因此它的文本中包含许多此类句子。

For each row I need to get the data ABC-####-#### , respectivelly I only need the last part. 对于每一行,我需要获取数据ABC-####-#### ,分别地,我只需要最后一部分。 So eg for ABC-2010-4091 I need to obtain 4091 . 因此,例如对于ABC-2010-4091我需要获取4091 This number I will need to join to other table. 我需要将此号码加入其他表。 I guess it would be enough to get the last parts of the format ABC-####-#### , then I should be able to handle the request. 我想这足以获取ABC-####-####格式的最后一部分,那么我应该能够处理该请求。

So the example of given above, the result should be 4091, 0586, 0587, 0604, 0562, 0570, 0575, 0588 in the row instead of the whole nvarchar value field. 因此,上面给出的示例,结果应为行中的4091, 0586, 0587, 0604, 0562, 0570, 0575, 0588而不是整个nvarchar值字段。

Is this possible somehow? 这有可能吗? The text in the nvarchar field differ, but the text format (ABC-####-####) I want to work with is still the same. nvarchar字段中的文本不同,但是我要使用的文本格式(ABC-####-####)仍然相同。 Only the count of characters for the last part may vary so its not only 4 numbers, but could be 5 or more. 仅最后部分的字符数可能会有所不同,因此它不仅是4个数字,而且可能是5个或更多。

What is the best approach to get these data? 获取这些数据的最佳方法是什么? Should I parse it in SSIS or on the SQL server side with SQL Query? 我应该在SSIS中还是在SQL Server端使用SQL Query解析它? And how? 如何?

I am aware this is though task. 我知道这是一项任务。 I appreciate every help or advice how to deal with this. 我感谢所有帮助或建议如何处理此问题。 I have not tried anything yet as I do not know where to start. 我还没有尝试过任何东西,因为我不知道从哪里开始。 I read articles about SQL parsing, but I want to ask for best approach to deal with this task. 我阅读了有关SQL解析的文章,但是我想寻求最佳方法来处理此任务。

Stackoverflow is about programming. Stackoverflow与编程有关。

Sit down and start programming. 坐下来开始编程。

Ok, seriously. 好吧,认真 That is string parsing and the last part in brackets with multiple fields means no bulk import, it is not a standard CSV file. 那是字符串解析,并且括号中具有多个字段的最后一部分意味着不批量导入,它不是标准的CSV文件。

Either you use SSIS in SQL Server and program the parsing there or.... you write a program for that. 您可以在SQL Server中使用SSIS并在其中编写解析程序,或者....为此编写程序。

String maniupation in SQL is the worst part of the language and I would avoid it. SQL中的字符串操作是该语言中最糟糕的部分,我会避免使用它。

So, yes, sit down and program a routine. 所以,是的,坐下来编写程序。 Probable the fastest way. 可能是最快的方法。

If I understand correctly, "ABS-####-####" will be the value coming through in the column and the numeric part is variable in length. 如果我理解正确,则“ ABS-####-####”将是该列中通过的值,数字部分的长度是可变的。

If that is the case, maybe this will work. 如果真是这样,也许这会起作用。

Use a "Derived Column" transformation. 使用“派生列”转换。 Lets say we call "ABC-####-####" = Column1 假设我们称“ ABC-####-####” = Column1

SUBSTRING("Column1",(FINDSTRING("Column1","-",2)+1),LEN(Column1)-(FINDSTRING("Column1","-",2)))

If I am not mistaken, that should give you the last # values in a new column no matter how long that value is. 如果我没记错的话,那么无论该值有多长,都应该为您提供新列中的最后#个值。

HTH 高温超导

I have worked this problem out with the following guides: 我已经通过以下指南解决了这个问题:

Split Multi Value Column into Multiple Records 将多值列拆分为多个记录
&
Remove Multiple Spaces with Only One Space 仅删除一个空格即可删除多个空格

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

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