Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Convert snippet from VBA to SQL

Tags:

sql

vba

vb6

t-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;
like image 402
erasmo carlos Avatar asked Jan 20 '26 10:01

erasmo carlos


1 Answers

REPLACE(yourstringcolumn, '.', '')

Replace on MSDN

like image 95
Jeffrey Van Laethem Avatar answered Jan 23 '26 00:01

Jeffrey Van Laethem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!