SQL help

ganesh21

2[H]4U
Joined
Jan 22, 2006
Messages
2,373
I need to get a part of a string in T-SQL.

Below is are two example of the string.

'\\path123\Imagename\abc123\date\filename.jpg'
'\\path567890\Imagename\abc123\date\filename.jpg'

the result i would like to get for each is:
path123
path567890

i try to use substring but sometime i get only part of the path.
 
You can use regular expressions to get it, or something like "explode" from PHP...

eg:

$temp = explode("\", $pathvar);

$goodpath = temp[2];

or for the regular expression something like

pregmatch("/\\\\(.*?)\\/", $pathvar, $matches);
$goodpath = $matches[1];

I think, can't test the above atm but it should be close.. assuming you can do it in php or some other language.
 
You can use regular expressions to get it, or something like "explode" from PHP...

eg:

$temp = explode("\", $pathvar);

$goodpath = temp[2];

or for the regular expression something like

pregmatch("/\\\\(.*?)\\/", $pathvar, $matches);
$goodpath = $matches[1];

I think, can't test the above atm but it should be close.. assuming you can do it in php or some other language.

Thank you, but i have to do this in SQL only.
 
Let me know if you have any questions.

Code:
DECLARE @Foo AS VARCHAR(80)
DECLARE @Start AS INTEGER

SET @Foo = 'blahblah\\path123\Imagename\abc123\date\filename.jpg';
-- SET @FOO = '\\path567890\Imagename\abc123\date\filename.jpg';
-- SET @Foo = 'blahblahpath123Imagenameabc123datefilename.jpg';

SET @Start = PATINDEX('%\\%\%', @Foo);
IF @Start = 0 
	PRINT 'Not found';
ELSE
BEGIN
	DECLARE @Temp AS VARCHAR(80);
	DECLARE @End AS INT;
	SET @Temp = SUBSTRING(@Foo, @Start+2, LEN(@Foo) );
	SET @End = PATINDEX('%\%', @Temp);
	IF @End = 0
		PRINT 'Not found';
	ELSE
	BEGIN
		SET @Temp = SUBSTRING(@Temp, 0, @End );
		PRINT '''' + @Temp + '''';
	END
END
 
Just because I'm on a sql kick, here is the answer in just a select to avoid using a proc. I tested with MSSQL not sure with Mysql. And it'll only work with UNC path's like the examples you gave..

Code:
SELECT SUBSTRING([NETWORK_PATH], 3, PATINDEX('%\%', RIGHT([NETWORK_PATH], LEN([NETWORK_PATH]) - 2)) -1)
 
Just because I'm on a sql kick, here is the answer in just a select to avoid using a proc. I tested with MSSQL not sure with Mysql. And it'll only work with UNC path's like the examples you gave..
This returns unpredictable results if the input is ill-formed. You'll want to shape it up for use in production.
 
Of course, but I was assuming he can trust the data, if he doesn't want to see the result he could easily just add a where clause,

Code:
WHERE [NETWORKPATH] LIKE '\\%\%'

if he wants to get an error for that row instead he could do

Code:
SELECT CASE 
	WHEN PATINDEX('\\%\%', [COLUMN]) = 1 THEN 
		SUBSTRING([COLUMN], 3, PATINDEX('%\%', RIGHT([COLUMN], LEN([COLUMN]) - 2)) -1)
	ELSE 'Yo this isn''t right' END

This will work for MSSQL I'm not sure about case statements in select statements in MySQL.

My way will also work for a table, instead of a proc for each individual UNC path. Unless you were suggesting a UDF.

And I'm not saying one way is better than the other, just been doing a lot of SQL lately and just throwing out different options.
 
Certainly, the SELECT-able statement is preferred!
 
Back
Top