saquery.com
a web developer' s kung fu…
Get minimum and maximum values of different sql datatypes
Categories: Sql

Everybody knows the min and max value properties in .Net.

Dim i As Int32 = Int32.MaxValue

The values represents the smallest and the largest value for a datatype. For some of us it might be interesting to get this information within a stored procedure.

Show Sql Source Code for SAQ_GET_DOMAIN_LIMIT.

Stored Procedure source code

call sa_make_object('function', 'SAQ_GET_DOMAIN_LIMIT');
ALTER FUNCTION "DBA"."SAQ_GET_DOMAIN_LIMIT"($domainName long varchar, $min bit default 1, $width int default 0, $scale int default 0) returns long varchar
begin
    declare _result long varchar;
	case $domainName 
	when 'smallint' then 
        if $min=1 then    
            set _result = '-32768';
        else 
            set _result = '32767';
        end if
	when 'unsigned smallint' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '65535';
        end if
	when 'integer' then 
        if $min=1 then    
            set _result = '-2147483648';
        else 
            set _result = '2147483647';
        end if
	when 'unsigned integer' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '4294967295';
        end if
	when 'int' then 
        if $min=1 then    
            set _result = '-2147483648';
        else 
            set _result = '2147483647';
        end if
	when 'unsigned int' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '4294967295';
        end if
	when 'tinyint' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '255';
        end if
	when 'unsigned tinyint' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '255';
        end if
	when 'bigint' then 
        if $min=1 then    
            set _result = '-9223372036854775808';
        else 
            set _result = '9223372036854775807';
        end if
	when 'unsigned bigint' then 
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '18446744073709551615';
        end if
	when 'bit' then
        if $min=1 then    
            set _result = '0';
        else 
            set _result = '1';
        end if
	when 'decimal' then
        set _result = replace(space($width-$scale),' ','9')||'.'||replace(space($scale),' ','9');
        if $min=1 then    
            set _result = '-'||_result;
        end if
	when 'double' then
        set _result = replace(space($width-$scale),' ','9')||'.'||replace(space($scale),' ','9');
        if $min=1 then    
            set _result = '-'||_result;
        end if
	when 'float' then
        set _result = replace(space($width-$scale),' ','9')||'.'||replace(space($scale),' ','9');
        if $min=1 then    
            set _result = '-'||_result;
        end if
	when 'real' then
        set _result = replace(space($width-$scale),' ','9')||'.'||replace(space($scale),' ','9');
        if $min=1 then    
            set _result = '-'||_result;
        end if
	when 'numeric' then
        set _result = replace(space($width-$scale),' ','9')||'.'||replace(space($scale),' ','9');
        if $min=1 then    
            set _result = '-'||_result;
        end if
    end;
    if right (_result,1)='.' then set _result = replace (_result, '.', '') end if;
    return _result;
end;

Stored Procedure Test

select *,
    SAQ_GET_DOMAIN_LIMIT(DOMAIN_NAME,1,"precision",1) as MIN_VALUE,
    SAQ_GET_DOMAIN_LIMIT(DOMAIN_NAME,0,"precision",1) as MAX_VALUE
    from sysdomain

Stored Procedure Resultset

domain_iddomain_nametype_idprecisionMIN_VALUEMAX_VALUE
1smallint55-3276832767
2integer410-21474836482147483647
3numeric2-.9.9
4float77-999999.9999999.9
5double815-99999999999999.999999999999999.9
6date9
7char1
8char1
9varchar12
10long varchar-1
11binary-2
12long binary-4
13timestamp11
14time10
19tinyint-630255
20bigint-520-92233720368547758089223372036854775807
21unsigned int-91004294967295
22unsigned smallint-105065535
23unsigned bigint-1121018446744073709551615
24bit-7101
27decimal2-.9.9
28varbinary-2
29uniqueidentifier-15
30varbit-16
31long varbit-17
32xml-18
33nchar-12
34nchar-12
35nvarchar-13
36long nvarchar-14

Vote this page

Leave a Reply