Wednesday, 2 October 2013

Collect a specific text string from a very large text string in SQL Server

Collect a specific text string from a very large text string in SQL Server

I have a very large text string in a column (column name: path) of a table
(table name: KYC_Path). The table basically contains very large text
string of image paths from which I need to extract a specific number (file
number). The text strings look like this:
F:\Original\15561 done\sohan1
15561\output_after_name_mapping\01710043429(1).jpg
T:\ALL BACKUP\BACK UP 1TB(SERVER)\ALL SCANNED FILES(sohan)\01832876657
(1).JPG
T:\ALL BACKUP\form 2TB passport HDD\All backup\Afren\24313\24313
Nupur_1box 651\output_after_name_mapping\01918082763(1).jpg
T:\ALL BACKUP\zaman all scan\16830 noboni1
box548\output_after_name_mapping\01823152145(1).jpg
I need to collect just following numbers (file name with out the (1).jpg
part), so that my new column will look like this:
01710043429
01832876657
01918082763
01823152145
I have already devised a method where use following formula:
select (right ([path],18)) as 'wallet_path', [path] into KYCPathNew
from [dbo].[KYCPath]
select (left ([wallet_path],11)) as 'wallet_path_new', [path] into
KYCPathNew2
from [dbo].[KYCPathNew]
However that gives some issues to some rows where file name do not come
correctly due to spaces between numbers (01823152145) and file extension
((1).jpg). And misses one or two characters from the file name.
Is there any other way so that I can collect all the file names more
efficiently? Can I make SQL Server find the last back slash and then take
11 digits from there? How?

No comments:

Post a Comment