Understanding max() representation of a string in Snowflake

654 Views Asked by At

Lets say I have two strings in a snowflake table column.

val
"000000001bb1304f"
"000000001bb13059"

I am trying to understand how MAX() works on these values. Is it decomposing each string into it's default encoding value (assuming Snowflake uses a specific encoding like utf-8 for STRING data type by default) and summing the total string value?

In my example I receive "000000001bb13059" as being the "max string" value but would like to understand how exactly that is decided.

Sample snowflake sql code:

-- direct comparison among strings
-- result is true
select '000000001bb1304f' < '000000001bb13059';

-- using MAX()
create temporary table test (val STRING);
insert into test values('000000001bb1304f');
insert into test values('000000001bb13059');

-- max val is '000000001bb13059'
select max(val) from test;
1

There are 1 best solutions below

0
NickW On

String comparison works by comparing the ascii value of each character - left to right. Your two strings are identical until the second to last char where you have '4' and '5', so the string with '5' is greater and so it returned by the MAX value.

BTW this is how string comparison works in most systems, it's not specific to Snowflake