Arithmetic operators
Operators
Translation for Arithmetic Operators
Conversion Table
Sample Source Patterns
Addition, Subtraction, Positive & Negative
Input Code:
CREATE TABLE test_math_operations (
base_value DECIMAL(10, 2),
multiplier INT,
divisor INT,
description VARCHAR(100),
created_at TIMESTAMP,
category VARCHAR(50)
);
INSERT INTO test_math_operations (base_value, multiplier, divisor, description, created_at, category)
VALUES
(100.50, 2, 5, 'Basic test', '2024-12-01 10:30:00', 'Type A'),
(250.75, 3, 10, 'Complex operations', '2024-12-02 15:45:00', 'Type B'),
(-50.25, 5, 8, 'Negative base value', '2024-12-03 20:00:00', 'Type C'),
(0, 10, 2, 'Zero base value', '2024-12-04 09:15:00', 'Type D');
SELECT +base_value AS positive_value,
-base_value AS negative_value,
(base_value + multiplier - divisor) AS add_sub_result,
created_at + INTERVAL '1 day' AS next_day,
created_at - INTERVAL '1 hour' AS one_hour_before,
description + category as string_sum,
base_value + '5' as int_string_sum,
'5' + base_value as string_int_sum
FROM test_math_operations;
Output Code:
CREATE TABLE test_math_operations (
base_value DECIMAL(10, 2),
multiplier INT,
divisor INT,
description VARCHAR(100),
created_at TIMESTAMP,
category VARCHAR(50)
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": { "major": 0, "minor": 0, "patch": "0" }, "attributes": { "component": "redshift", "convertedOn": "12/16/2024", "domain": "test" }}';
INSERT INTO test_math_operations (base_value, multiplier, divisor, description, created_at, category)
VALUES
(100.50, 2, 5, 'Basic test', '2024-12-01 10:30:00', 'Type A'),
(250.75, 3, 10, 'Complex operations', '2024-12-02 15:45:00', 'Type B'),
(-50.25, 5, 8, 'Negative base value', '2024-12-03 20:00:00', 'Type C'),
(0, 10, 2, 'Zero base value', '2024-12-04 09:15:00', 'Type D');
SELECT +base_value AS positive_value,
-base_value AS negative_value,
(base_value + multiplier - divisor) AS add_sub_result,
created_at + INTERVAL '1 day' AS next_day,
created_at - INTERVAL '1 hour' AS one_hour_before,
description || category as string_sum,
base_value + '5' as int_string_sum,
'5' + base_value as string_int_sum
FROM
test_math_operations;
Exponentiation, multiplication, division & modulo
Input Code:
CREATE TABLE test_math_operations (
base_value DECIMAL(10, 2),
multiplier INT,
divisor INT,
mod_value INT,
exponent INT
);
INSERT INTO test_math_operations (base_value, multiplier, divisor, mod_value, exponent)
VALUES
(100.50, 2, 5, 3, 2),
(250.75, 3, 10, 7, 3),
(-50.25, 5, 8, 4, 4),
(0, 10, 2, 1, 5);
SELECT
base_value ^ exponent AS raised_to_exponent,
(base_value * multiplier) AS multiplied_value,
(base_value / divisor) AS divided_value,
base_value::int / divisor as int_division,
(mod_value % 2) AS modulo_result,
(base_value + multiplier - divisor) AS add_sub_result,
(base_value + (multiplier * (divisor - mod_value))) AS controlled_eval
FROM
test_math_operations;
Output Code:
CREATE TABLE test_math_operations (
base_value DECIMAL(10, 2),
multiplier INT,
divisor INT,
mod_value INT,
exponent INT
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": { "major": 0, "minor": 0, "patch": "0" }, "attributes": { "component": "redshift", "convertedOn": "12/10/2024", "domain": "test" }}';
INSERT INTO test_math_operations (base_value, multiplier, divisor, mod_value, exponent)
VALUES
(100.50, 2, 5, 3, 2),
(250.75, 3, 10, 7, 3),
(-50.25, 5, 8, 4, 4),
(0, 10, 2, 1, 5);
SELECT
POWER(
base_value, exponent) AS raised_to_exponent,
(base_value * multiplier) AS multiplied_value,
(base_value / divisor) AS divided_value,
FLOOR(
base_value::int / divisor) as int_division,
(mod_value % 2) AS modulo_result,
(base_value + multiplier - divisor) AS add_sub_result,
(base_value + (multiplier * (divisor - mod_value))) AS controlled_eval
FROM
test_math_operations;
Absolute value, Square root and Cube root
Input Code:
CREATE TABLE unary_operators
(
col1 INTEGER,
col2 INTEGER
);
INSERT INTO unary_operators VALUES
(14, 10),
(-8, 8),
(975, 173),
(-1273, 187);
SELECT
|/ col2 AS square_root,
||/ col1 AS cube_root,
@ col1 AS absolute_value
FROM unary_operators;
Output Code:
CREATE TABLE unary_operators
(
col1 INTEGER,
col2 INTEGER
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": { "major": 0, "minor": 0, "patch": "0" }, "attributes": { "component": "redshift", "convertedOn": "12/17/2024", "domain": "test" }}';
INSERT INTO unary_operators
VALUES
(14, 10),
(-8, 8),
(975, 173),
(-1273, 187);
SELECT
SQRT(col2) AS square_root,
CBRT(col1) AS cube_root,
ABS(col1) AS absolute_value
FROM
unary_operators;
Known Issues
In Snowflake, it is possible to use the unary operators
+
and-
with string values, however in Redshift it is not valid.
Related EWIs
No related EWIs.
Last updated