SSIS How to get part of a string by separator
Asked Answered
F

4

16

I need an SSIS expression to get the left part of a string before the separator, and then put the new string in a new column. I checked in derived column, it seems no such expressions. Substring could only return string part with fixed length.

For example, with separator string - :

Art-Reading                Should return Art
Art-Writing                Should return Art
Science-chemistry          Should return Science

P.S. I knew this could be done in MySQL with SUBSTRING_INDEX(), but I'm looking for an equivalent in SSIS, or at least in SQL Server

Fail answered 6/6, 2012 at 20:21 Comment(1)
possible duplicate of Help with SubString in SSISFail
D
25

of course you can:

enter image description here

just configure your derived columns like this:

enter image description here

Here is the expression to make your life easier:

SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)

FYI, the second "1" means to get the first occurrence of the string "-"

EDIT: expression to deal with string without "-"

FINDSTRING(name,"-",1) != 0 ? (SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)) : name
Diffusive answered 7/6, 2012 at 9:27 Comment(2)
Thx a lot for the answer!! However, some values of the "name" column do not have a seperator and in this case, I wish the "new_name" column will be the same as "name". E.g. one of the name is "Science", and the new_name would just be "Science". So the whole process would be like this: if name has a seperator, then use the solution, else, copy from name to new_nameFail
that can easily be solved with a conditional expression. See my edit!Diffusive
W
32

Better late than never, but I wanted to do this too and found this.

TOKEN(character_expression, delimiter_string, occurrence)

TOKEN("a little white dog"," ",2)

returns little the source is below

http://technet.microsoft.com/en-us/library/hh213216.aspx

Waldemar answered 16/1, 2014 at 5:48 Comment(2)
This is probably the best answer here.Lodging
I have been looking for this forever!! Best answerGranulite
D
25

of course you can:

enter image description here

just configure your derived columns like this:

enter image description here

Here is the expression to make your life easier:

SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)

FYI, the second "1" means to get the first occurrence of the string "-"

EDIT: expression to deal with string without "-"

FINDSTRING(name,"-",1) != 0 ? (SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)) : name
Diffusive answered 7/6, 2012 at 9:27 Comment(2)
Thx a lot for the answer!! However, some values of the "name" column do not have a seperator and in this case, I wish the "new_name" column will be the same as "name". E.g. one of the name is "Science", and the new_name would just be "Science". So the whole process would be like this: if name has a seperator, then use the solution, else, copy from name to new_nameFail
that can easily be solved with a conditional expression. See my edit!Diffusive
L
9

You can specify the length to copy in the SUBSTRING function and check for the location of the dash using CHARINDEX

SELECT SUBSTRING(@sString, 1, CHARINDEX('-',@sString) - 1)

For the SSIS expression it is pretty much the same code:

SUBSTRING(@[User::String], 1, FINDSTRING(@[User::String], "-", 1)-1)
Liveried answered 6/6, 2012 at 20:30 Comment(0)
A
2

if SUBSTRING length param returns -1 then it results in error, "The length -1 is not valid for function "SUBSTRING". The length parameter cannot be negative. Change the length parameter to zero or a positive value."

Aisha answered 24/9, 2015 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.