vendredi 24 juin 2016

VBA: Convert snippet from VBA to SQL

I have a small If statement that I would like to ask for help to convert it to SQL syntax.

If I am correct, the If statement checks for the position where a period on the string passed might be found. If found (> 0) e.g. string: "VQAL1-SQ994.1", would be 12 so > 0 would be true, then can someone explain to me what the output of: Id = Left(ID, InStr(ID, ".") - 1) wuld be given the example string?

If InStr(ID, ".") > 0 Then 
    Id = Left(ID, InStr(ID, ".") - 1)
End If

Thank you for your help.

UPDATE

I put together a small stored procedure to see if I was able to make this work, but so far I think it is not working because it returns nothing from the string I pass.

Test execute:

/*
EXEC Test_String  'VQAL1-SQ994.1'
*/

Stored Procedure contents:

ALTER PROCEDURE [dbo].[Test_String] (@ProductID VARCHAR(25))
AS
BEGIN

    DECLARE @ID VARCHAR (25)
    SET @ID = (
                      SELECT
                        LEFT(@ProductID, LEN(@ProductID) - LEN(REPLACE(@ProductID, '.', '')) - 1)
                     );

    SELECT @ID;
END;

Aucun commentaire:

Enregistrer un commentaire