tags:

views:

125

answers:

5

I have a row of strings that are in the following format:

'Order was assigned to lastname,firsname'

I need to cut this string down into just the last and first name but it is always a different name for each record.

The 'Order was assigned to' part is always the same.......

Thanks

I am using SQL Server. It is multiple records with different names in each record.

+2  A: 

In your specific case you can use something like:

SELECT SUBSTRING(str, 23) FROM table

However, this is not very scalable, should the format of your strings ever change.

If you are using an Oracle database, you would want to use SUBSTR instead.


Edit:

For databases where the third parameter is not optional, you could use SUBSTRING(str, 23, LEN(str))

Somebody would have to test to see if this is better or worse than subtraction, as in Martin Smith's solution but gives you the same result in the end.

MikeD
Done you need 3 arguments in a Substring?
AFAIK, the third argument (length) is optional. With only two arguments it will go from start position to the end.Like everything else in the SQL world, this might depend on the particular implementation, though.
MikeD
Nope. In SQL Server `Msg 174, Level 15, State 1, Line 7 The substring function requires 3 argument(s).`
Martin Smith
For SQL Server (at least 2008) this is not the case. The third parameter is required.
Tom H.
3rd is optional. - http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr
RobertPitt
@Robert No it isn't. I've tried it in SQL Server and it gives the following `Msg 174, Level 15, State 1, Line 7 The substring function requires 3 argument(s).` Edit: They aren't using MySQL!
Martin Smith
+1  A: 

For SQL Server

   WITH testData AS
    (
    SELECT 'Order was assigned to lastname,firsname' as Col1 UNION ALL
    SELECT 'Order was assigned to Bloggs, Jo' as Col1
    )

    SELECT SUBSTRING(Col1,23,LEN(Col1)-22) AS Name
    from testData

Returns

Name

---------------------------------------
lastname,firsname
Bloggs, Jo
Martin Smith
A: 

I would require that a colon or some other delimiter be between the message and the name. Then you could just search for the index of that character and know that anything after it was the data you need...

Example with format changing over time:

CREATE TABLE #Temp (OrderInfo NVARCHAR(MAX))
INSERT INTO #Temp VALUES ('Order was assigned to :Smith,Mary')
INSERT INTO #Temp VALUES ('Order was assigned to :Holmes,Larry')
INSERT INTO #Temp VALUES ('New Format over time :LootAt,Me')


SELECT      SUBSTRING(OrderInfo, CHARINDEX(':',OrderInfo)+1, LEN(OrderInfo))
FROM        #Temp


DROP TABLE #Temp
Dining Philanderer
What's the point of that? They already know the index!
Martin Smith
So that they can change whatever the message at the front is to whatever in the future. The rule being that the data is after the delimiter...
Dining Philanderer
A: 

on MS SQL Server:

declare @str varchar(100) = 'Order was assigned to lastname,firsname'
declare @strLen1 int = DATALENGTH('Order was assigned to ')
declare @strLen2 int = len(@str)
select @strlen1, @strLen2, substring(@str,@strLen1,@strLen2), 
                  RIGHT(@str, @strlen2-@strlen1)
TheVillageIdiot
+1  A: 

In addition to the SUBSTRING methods, you could also use a REPLACE function. I don't know which would have better performance over millions of rows, although I suspect that it would be the SUBSTRING - especially if you were working with CHAR instead of VARCHAR.

SELECT REPLACE(my_column, 'Order was assigned to ', '')
Tom H.