question

Charles Broadfoot avatar image
Charles Broadfoot asked

Selecting column with multiple functions

I'm trying to select a column with multiple functions and I dont know how. Lets say that my column's data returns data with values such as 'Foo_RockStar.Complete' and what I want to extract is the 'RockStar'. There could be lots of pre-fixes such as the 'Foo_' and lots of endings such as the '.Complete', so my initial query looked somthing like.... SELECT SUBSTRING([column1],4,20) REPLACE([column1],'.Complete','') AS [DesiredText] FROM MYTable WHERE [column1] like '%.Complete' The Desired outcome would be somthing like 'RockStar'
t-sqlqueryfunctions
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tim avatar image
Tim answered
To answer your question about syntax of your SELECT statement, it would be something more like SELECT replace(SUBSTRING(@row,4,20),'.Complete','') AS [DesiredText] ... Are you looking for a solution that would account for the multiple prefixes and endings or does the query above meet your needs? This should work for what you need just replace @row with your column name in the select statement, to test the statement I had to create a variable to pass to it.: DECLARE @row varchar(50) SET @row = 'Foo_RockStar.Complete' SELECT SUBSTRING(@row,((CHARINDEX('_',@row)+1)),(CHARINDEX('.',@row)-(CHARINDEX('_',@row)+1)))
7 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Charles Broadfoot avatar image Charles Broadfoot commented ·
I'm actualy looking for the ability to use more than one function on the same column. multiple prefixes and multiple endings exists with varing lengths
0 Likes 0 ·
Tim avatar image Tim commented ·
Does the prefix always end with _ and the ending come after a (.)period?
0 Likes 0 ·
Charles Broadfoot avatar image Charles Broadfoot commented ·
yes, but there could be more than one of each in the name
0 Likes 0 ·
Charles Broadfoot avatar image Charles Broadfoot commented ·
so like first index of _ and last index of . would work
0 Likes 0 ·
Tim avatar image Tim commented ·
So every scenario would be something_YOURVALUE.ending Where 'something' could be of various lengths and 'ending' could be one of many words.
0 Likes 0 ·
Show more comments
Scot Hauder avatar image
Scot Hauder answered
DECLARE @COLUMN varchar(50) SET @COLUMN = 'prefix_Foo_RockStar.Complete.suffix' SELECT SUBSTRING(@COLUMN,2+LEN(@COLUMN)-CHARINDEX('_',REVERSE(@COLUMN)) ,CHARINDEX('.',@COLUMN)-(2+LEN(@COLUMN)-CHARINDEX('_',REVERSE(@COLUMN))))
8 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tim avatar image Tim commented ·
@scot Your solution like mine above only works when the column has a single prefix and suffix, however his column can have multiple prefixes and suffixes. IE - something_YOURVALUE.ending or somthing_something_somthing_YOURVALUE.ending.ending Any ideas?
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
Try this one
0 Likes 0 ·
Tim avatar image Tim commented ·
Great Job @Scot +1. I love trying to help someone and learning something new myself.
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
I learn a lot here myself and enjoy these challenges. Although the real answer to this is question is to normalize the table and create a separate column for YOURVALUE
0 Likes 0 ·
Tim avatar image Tim commented ·
Agreed, he has some very bad data inputs going on. Many times though we have no control over the data we manage. I just got through stepping through you code, once you break the individual segments down it is pretty simple logic. I will be adding this little snippet of code to my code bank as I am sure I will need something like this at some point in the future. Tonight is really the first time I have used charindex. I have known about it and known its usage but haven't ever used it before.
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
I've seen this a lot--knowledge built into a string or invoice number/customer number and you're right, we always arrive after that fact and can't change the schema
0 Likes 0 ·
Tim avatar image Tim commented ·
@Scot, curious about something for my own knowledge and education. To get the number of characters to the period(.) you chose to use PATINDEX instead of CHARINDEX in that situation only. Why go with PATINDEX? Wouldn't CHARINDEX still achieve the same goal? I figure you had a reason, I am just not that familiar with the functions. I tested your solution with PATINDEX and CHARINDEX and got the same result. I figure there is a gotcha I am not aware of.
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
PATINDEX was part of a previous solution when I wasn't clear of the OP's requirement. As you mention, it is suffice to use CHARINDEX and I actually changed it to that almost immediately after posting which makes me think you saw the PATINDEX in your email...
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.