SQL Puzzle based on arithmetic operations | SQL Scenario Interview Question

preview_player
Показать описание
DROP TABLE IF EXISTS Operation;

CREATE TABLE Operation
(
[Index] INT,
[Value] INT,
[Formula] nvarchar(10)

)

INSERT INTO Operation
VALUES
(1,10,'4+5'),
(2,20,'7-3'),
(3,30,'9-6'),
(4,40,'2+8'),
(5,50,'7+6'),
(6,60,'6-8'),
(7,70,'9*10'),
(8,80,'3*7'),
(9,90,'7/10'),
(10,100,'10/3')

SELECT * FROM Operation
Рекомендации по теме
Комментарии
Автор

Hi admin, I use below method to get the CTE result

SELECT *
, IIF(LEN(Formula) = 3
, LEFT(Formula,1)
, IIF(LEN(TRIM(TRANSLATE(LEFT(Formula,2),'0123456789',' '))) = 0, LEFT(Formula, 2), LEFT(Formula, 1))
) AS left_operand
, IIF(LEN(Formula) = 3
, RIGHT(Formula,1)
, IIF(LEN(TRIM(TRANSLATE(RIGHT(Formula,2),'0123456789',' '))) = 0, RIGHT(Formula, 2), RIGHT(Formula, 1))
) AS right_operand
FROM Operation

FromPlanetZX