## A simple math expression solver

 Author Message pierre-702284 SSC-Enthusiastic Group: General Forum Members Points: 110 Visits: 143 If the specification is for a single operation with an integer on either side ...CREATE FUNCTION [SimpleMath](@expression VARCHAR(max))RETURNS INTASBEGIN DECLARE @position INT, @operator CHAR(1), @left INT, @right INT, @result INT SELECT @position = PATINDEX('%[*/+-]%', @expression), @operator = SUBSTRING(@expression, @position, 1), @left = CONVERT(INT, LTRIM(RTRIM(LEFT(@expression, @position - 1)))), @right = CONVERT(INT, LTRIM(RTRIM(RIGHT(@expression, LEN(@expression) - (@position + 1))))), @result = CASE @operator WHEN '*' THEN @left * @right WHEN '/' THEN @left / @right WHEN '+' THEN @left + @right WHEN '-' THEN @left - @right END RETURN @resultENDGO;WITH [data] ([expression]) AS ( SELECT '1 + 5' UNION ALL SELECT '10 * 4' UNION ALL SELECT '15 / 3' UNION ALL SELECT '100 - 25' )SELECT [expression], [result] = [dbo].[SimpleMath]([expression])FROM [data]