October 24, 2010 at 1:25 pm
Hello Everyone
I am working on some things, and ran across this to try and figure out. This is very close to my other post that Wildcat answered.
I have a string that looks like this:
10494B-XL-Large.jpg
What I am working on is to write some code that will return only this:
10494B-XL
Each string has two hyphens, but not always in the same character location.
Does anyone have anything that will work that I can try?
Thanks
Andrew SQLDBA
October 24, 2010 at 1:33 pm
You want to remove the second hyphen and everything after it?
If there will only ever be two hyphens, reverse the string, take everything to the right of the first hyphen, then reverse the result.
REVERSE, CHARINDEX, LEN and RIGHT are the functions that you'll be needing.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 24, 2010 at 1:37 pm
Gail
What a great idea. Let me work on that to see if I can get it.
Thank you
Andrew SQLDBA
October 24, 2010 at 1:49 pm
Old, old trick. Most commonly used for extracting just the file name or just the folders from a full path - C:\Some\Odd\Dir\Thisfile.txt
Not just a T-SQL trick.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 24, 2010 at 1:52 pm
I am coming close, but there is something that I am missing. I will keep at it to see if I can figure it out.
Thanks for your help in pointing me in the correct direction
AndrewSQLDBA
October 24, 2010 at 2:02 pm
I lied, no need for LEN. :hehe:
DECLARE @FileName VARCHAR(50) = '10494B-XL-Large.jpg';
SELECT REVERSE(RIGHT(REVERSE(@FileName), CHARINDEX('-', REVERSE(@FileName) ,0)-1));
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 24, 2010 at 7:13 pm
If there's always two dashes, REVERSE (especially two reverses) is a bit expensive. This will work if there's always two dashes and be a bit faster.
DECLARE @FileName VARCHAR(50)
SELECT @FileName = '10494B-XL-Large.jpg';
SELECT SUBSTRING(@FileName,1,CHARINDEX('-',@FileName,CHARINDEX('-',@FileName)+1)-1)
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply