Arithmetic operators
Operators
Translation for Arithmetic Operators
Conversion Table
Fully supported by Snowflake
Fully supported by Snowflake
Fully supported by Snowflake
Redshift division between integers always returns integer value, FLOOR function is added to emulate this behavior.
Fully supported by Snowflake
Fully supported by Snowflake. When string are added, it is transformed to a concat.
Fully supported by Snowflake
Fully supported by Snowflake
Fully supported by Snowflake
Fully supported by Snowflake
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;
100.50
-100.50
97.50
2024-12-02 10:30:00.000000
2024-12-01 09:30:00.000000
Basic testType A
250.75
-250.75
243.75
2024-12-03 15:45:00.000000
2024-12-02 14:45:00.000000
Complex operationsType B
-50.25
50.25
-53.25
2024-12-04 20:00:00.000000
2024-12-03 19:00:00.000000
Negative base valueType C
0.00
0.00
8.00
2024-12-05 09:15:00.000000
2024-12-04 08:15:00.000000
Zero base valueType D
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;
2024-12-02 10:30:00
2024-12-01 09:30:00
Basic testType A
2024-12-03 15:45:00
2024-12-02 14:45:00
Complex operationsType B
2024-12-04 20:00:00
2024-12-03 19:00:00
Negative base valueType C
2024-12-05 09:15:00
2024-12-04 08:15:00
Zero base valueType D
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;
+-------------------+--------------------+--------------+
|square_root |cube_root |absolute_value|
+-------------------+--------------------+--------------+
|3.1622776601683795 |2.4101422641752306 |14 |
|2.8284271247461903 |-2 |8 |
|13.152946437965905 |9.915962413403873 |975 |
|13.674794331177344 |-10.837841647592736 |1273 |
+-------------------+--------------------+--------------+
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;
+-------------+--------------+--------------+
|square_root |cube_root |absolute_value|
+-------------+--------------+--------------+
|3.16227766 |2.410142264 |14 |
|2.828427125 |-2 |8 |
|13.152946438 |9.915962413 |975 |
|13.674794331 |-10.837841648 |1273 |
+-------------+--------------+--------------+
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