I have a column called original_filename which contains text. within the text there is a file ID which I want to extract. The description starts with "error_rows_" as shown below. However the file ID's vary in length i.e 999 to 999999 followed by additional text.
error_rows_90349_2014-04-10_00-00-00_Transaction-Login_TheHut.gpg
error_rows_904003_2014-04-10_12-00-00_Transaction-Login_TheHut.gpg
error_rows_90403_2014-04-10_12-00-00_Transaction-Registration_TheHut.gpg
error_rows_9060_2014-04-11_00-00-00_Transaction-Login_TheHut.gpg
Can someone help me with the sql to extract this. the end result should be:
90349 904003 90403 9060
I have been trying to use charindex withou success
Try this:
DECLARE @Test TABLE (TestData VARCHAR(500));
INSERT INTO @Test SELECT 'error_rows_90349_2014-04-10_00-00-00_Transaction-Login_TheHut.gpg';
INSERT INTO @Test SELECT 'error_rows_904003_2014-04-10_12-00-00_Transaction-Login_TheHut.gpg';
INSERT INTO @Test SELECT 'error_rows_90403_2014-04-10_12-00-00_Transaction-Registration_TheHut.gpg';
INSERT INTO @Test SELECT 'error_rows_9060_2014-04-11_00-00-00_Transaction-Login_TheHut.gpg';
SELECT
*,
CONVERT(INT, SUBSTRING(TestData, 12, CHARINDEX('_', TestData, 12) - 12))
FROM
@Test;
Results:
90349
904003
90403
9060