Skip to content
Advertisement

Extract first argue from column with T-SQL

I need to extract the first argument: "PATHTOPACKAGE" from the string below:

/SQL "PATHTOPACKAGE" /SERVER "0.0.0.0,0000" /USER usr /PASSWORD "passw" /DECRYPT pass2 /CHECKPOINTING OFF /REPORTING E

which is a column stored at SQL Server msdb.sysjobsteps.command

Is there a way to do it by using Regex or Spliting it by blank spaces?

Advertisement

Answer

Based on the one example we have, a couple of CHARINDEXs a little bit of simple string manipulation gets the job done:

DECLARE @YourString nvarchar(500) = N'/SQL "PATHTOPACKAGE" /SERVER "0.0.0.0,0000" /USER usr /PASSWORD "passw" /DECRYPT pass2 /CHECKPOINTING OFF /REPORTING E';

SELECT LEFT(STUFF(V.YourString,1,CI1.I,''),CI2.I-CI1.I-1)
FROM (VALUES(@YourString))V(YourString)
     CROSS APPLY(VALUES(CHARINDEX(' ',V.YourString)))CI1(I)
     CROSS APPLY(VALUES(CHARINDEX(' /',V.YourString,CI1.I)))CI2(I);

If parameters don’t change position, then you can build a more robust solution.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement