I got the following entry in my database:
images/test.jpg
I want to trim the entry so I get: test
So basically, I want
----select characters before / including /
select SUBSTRING ('abcde/wxyz',0,CHARINDEX('/','abcde/wxyz')+1)
--select characters after / including /
select SUBSTRING('abcde/wxyz',CHARINDEX('/','abcde/wxyz'),LEN('abcde/wxyz'))
SELECT SUBSTRING('ravi1234@gmail.com',1,(CHARINDEX('@','ravi1234@gmail.com')-1)) Before, RIGHT('ravi123@gmail.com',(CHARINDEX('@','ravi123@gmail.com')+1)) After
If you want to get this out of your table using SQL, take a look at the following functions that will help you: SUBSTRING and CHARINDEX. You can use those to trim your entries.
A possible query will look like this (where col
is the name of the column that contains your image directories:
SELECT SUBSTRING(col, LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) + 1,
LEN(col) - LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) - LEN(SUBSTRING(
col, CHARINDEX ('.', col), LEN(col))));
Bit of an ugly beast. It also depends on the standard format of 'dir/name.ext'.
Edit:
This one (inspired by praveen) is more generic and deals with extensions of different length:
SELECT SUBSTRING(col, LEN(LEFT(col, CHARINDEX ('/', col))) + 1, LEN(col) - LEN(LEFT(col,
CHARINDEX ('/', col))) - LEN(RIGHT(col, LEN(col) - CHARINDEX ('.', col))) - 1);
I have made a method which is much more general :
so :
DECLARE @a NVARCHAR(MAX)='images/test.jpg';
--Touch here
DECLARE @keysValueToSearch NVARCHAR(4000) = '/'
DECLARE @untilThisCharAppears NVARCHAR(4000) = '.'
DECLARE @keysValueToSearchPattern NVARCHAR(4000) = '%' + @keysValueToSearch + '%'
--Nothing to touch here
SELECT SUBSTRING(
@a,
PATINDEX(@keysValueToSearchPattern, @a) + LEN(@keysValueToSearch),
CHARINDEX(
@untilThisCharAppears,
@a,
PATINDEX(@keysValueToSearchPattern, @a) + LEN(@keysValueToSearch)
) -(PATINDEX(@keysValueToSearchPattern, @a) + LEN(@keysValueToSearch))
)
I know this has been a while.. but here is an idea
declare @test varchar(25) = 'images/test.jpg'
select
@test as column_name
, parsename(replace(@test,'/','.'),1) as jpg
,parsename(replace(@test,'/','.'),2) as test
,parsename(replace(@test,'/','.'),3) as images
I got some invalid length errors. So i made this function, this should not give any length problems. Also when you do not find the searched text it will return a NULL.
CREATE FUNCTION [FN].[SearchTextGetBetweenStartAndStop](@string varchar(max),@SearchStringToStart varchar(max),@SearchStringToStop varchar(max))
RETURNS varchar(max)
BEGIN
SET @string = CASE
WHEN CHARINDEX(@SearchStringToStart,@string) = 0
OR CHARINDEX(@SearchStringToStop,RIGHT(@string,LEN(@string) - CHARINDEX(@SearchStringToStart,@string) + 1 - LEN(@SearchStringToStart))) = 0
THEN NULL
ELSE SUBSTRING(@string
,CHARINDEX(@SearchStringToStart,@string) + LEN(@SearchStringToStart) + 1
,(CHARINDEX(@SearchStringToStop,RIGHT(@string,LEN(@string) - CHARINDEX(@SearchStringToStart,@string) + 1 - LEN(@SearchStringToStart)))-2)
)
END
RETURN @string
END