0

Hi I have this value in a column

11-1-11111-349b65eda2-4f8e-413d-b76a-6a2c13d6e494OctWed0800422015-request-response.xml

How would I parse this using SQL to get only:

11-1-11111-349b65eda2-4f8e-413d-b76a-6a2c13d6e494OctWed0800422015
2
  • 1
    tell us what is the logic.for parse the string. Remove the text after 8th - ? Or maybe remove after length string > 65 Commented Oct 7, 2015 at 15:13
  • 1
    Please tag your question with the database you are using Commented Oct 7, 2015 at 15:17

1 Answer 1

1

In SQL-Server you could use this approach if the right side is always -request-response.xml:

SELECT FileName,
       Leftpart = LEFT(FileName,CHARINDEX('-request-response.xml',FileName)-1)
FROM MyTable

Demo

This is more fail-safe:

WITH CTE AS
(
   SELECT FileName,
          Length = CHARINDEX('-request-response.xml',FileName) -1
    FROM MyTable
)
SELECT LeftPart = CASE WHEN Length < 0 THEN NULL 
                   ELSE LEFT(FileName, Length) END
FROM CTE
Sign up to request clarification or add additional context in comments.

4 Comments

If you happen to be using a database that supports left() and charindex(), which pretty much means SQL Server or Sybase.
@GordonLinoff: i've added that this is for sql-server. Thought that it was but OP didn't mention it.
I get the error: Msg 537, Level 16, State 3, Line 2 Invalid length parameter passed to the LEFT or SUBSTRING function.
@JerryTrac: if the value doesnt contain the search word you'll get a negative length which is not allowed in LEFT. That's why i've added the second approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.