• Sorry if this overlaps already-posted code, was busy for a long while before I was able to get back to working on this:

    SELECT string, CASE WHEN next_to_last_space = 0 THEN ''

    ELSE SUBSTRING(string, LEN(string) - next_to_last_space + 2,

    next_to_last_space - last_space) END AS value

    FROM (

    VALUES('INV CRT IS15000467 1'),

    ('INV CSH 144934 1'),

    ('INV CSH IS15000134 1'),

    ('INV CSH IS15000442 10'),

    ('INV CSH IS15000442 xx q rrrrrr e10'),

    ('INV ERR'),

    ('INVE')

    ) AS test_data(string)

    CROSS APPLY (

    SELECT CHARINDEX(' ', REVERSE(string)) AS last_space

    ) AS assign_alias_names1

    CROSS APPLY (

    SELECT CASE WHEN last_space = 0 THEN 0

    ELSE CHARINDEX(' ', REVERSE(string), last_space + 1) END AS next_to_last_space

    ) AS assign_alias_names2

    Edit: Split the "ELSE" code in the main query to separate lines.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.