0

I have filename field in table which contains value as follows,

Its image files preceding with employee code

H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\e45432_Sign.jpg
H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\e45434_1418_Sign.jpg
H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\E45434_age_Sign.jpg
H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\E45538_Rana_Sign.jpg

Now I want to write SQL query in such a way that I will get only employee numbers from the path i.e. for example output will be in the following format

empno
e45432
e45434
E45538

I have tried with following query

 select SUBSTRING([FileName],55,6) as empno  from #tmp_filepath

it has given me the output I wanted but it's like I'm hardcoding the positions

1

1 Answer 1

3

Try this:

SELECT 
LEFT(
     RIGHT(Str,CHARINDEX('\',REVERSE(Str))-1),
     CHARINDEX('_',RIGHT(Str,CHARINDEX('\',REVERSE(Str))-1))-1
  )
FROM T

SQLFiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

declare @str varchar(50) select @str=right('H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\E35697_Sign.jpg', CHARINDEX('\',reverse('H:\KYC_PDF_Cloud_N\Employee\PANCard-CroppedSignatures\E35697_Sign.jpg'))-1) select left(@str,charindex('_',@str)-1)

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.