User Tools

Site Tools


postgresqlfunctions

SQL and PL/pgSQL

JSON

Arrays

Arrays can be displayed as ARRAY[1,2,3] or {1,2,3}.

Operators

SELECT 1 WHERE 'ian' = ANY(ARRAY['ian','bob']); --True
SELECT 1 WHERE 'ian' = ALL(ARRAY['ian','bob']); --False
-- Does each element appearing in the 2nd array equal some element of the 1st array
SELECT ARRAY[1,2,3] @> ARRAY[1,3];              --True
SELECT ARRAY[1,2,3] @> ARRAY[1,3,4];            --False
-- Does each element appearing in the 1st array equal some element of the 2nd array
SELECT ARRAY[1,3] <@ ARRAY[1,2,3];              --True
SELECT ARRAY[1,3,4] <@ ARRAY[1,2,3];            --False
-- Do the arrays have any elements in common.
SELECT ARRAY[1,2,3] && ARRAY[3,4];              --True
SELECT ARRAY[1,2,3] && ARRAY[4,5];              --False

Append / Prepend

SELECT ARRAY[1,2,3] || 4;
SELECT array_append(ARRAY[1,2,3], 4);
 
SELECT 0 || ARRAY[1,2,3];
SELECT array_prepend(0, ARRAY[1,2,3]);

Replace / Remove

SELECT array_replace(ARRAY[1,2,4], 4, 3);
SELECT array_remove(ARRAY[1,2,3,4], 4);

Concatenate

SELECT ARRAY[1,2,3] || 4;
SELECT 0 || ARRAY[1,2,3];
SELECT array_cat(ARRAY[0], ARRAY[1,2.3]);

Length

SELECT array_length(ARRAY[1,2,3], 1);
SELECT cardinality(ARRAY[1,2,3]);

Position

SELECT array_position(ARRAY[1,2,3], 2);
SELECT array_positions(ARRAY[1,2,2,3], 2);

Conversion

SELECT array_to_string(ARRAY[1,2,3], ',');         -- Returns 1 row (1,2,3)
SELECT unnest(ARRAY[1,2,3]);                       -- Returns 3 rows
SELECT generate_subscripts(ARRAY['A','B','C'], 1); -- Returns 3 rows
 
--Convert text column with comma separated values to array
ALTER TABLE name_basics ALTER primaryprofession TYPE text[] 
                        USING string_to_array(primaryprofession, ','); 

Templates

Functions

CREATE [OR REPLACE] FUNCTION fnc_nme()
   RETURNS <return type> 
   LANGUAGE plpgsql
AS $$
DECLARE 
-- Variables
BEGIN
 -- Logic
END; $$

Procedures

CREATE [OR REPLACE] PROCEDURE prc_nme()
LANGUAGE plpgsql
AS $$
DECLARE
-- Variables
BEGIN
-- Body
END; $$

If Then Else

IF condition_1 THEN
  statement_1;
ELSIF condition_n THEN
  statement_n;
ELSE
  else-statement;
END IF;

Case

CASE RATE
  WHEN 'a' THEN variable = 1;
  WHEN 'b' THEN variable = 2;
  ELSE PRICE_SEGMENT = 0;
END CASE;

CASE 
  WHEN total > 200 THEN variable = 'PLATINUM' ;
  WHEN total > 100 THEN variable = 'GOLD' ;
  ELSE variable = 'SILVER' ;
END CASE;

Loop

LOOP
   -- Statements
   IF condition THEN
      EXIT;
   END IF;
END LOOP;

WHILE CONDITION LOOP
   -- Statements;
END LOOP;

FOR loop_counter in [ reverse ] FROM.. TO [ by step ] LOOP
    -- Statements
END LOOP
postgresqlfunctions.txt · Last modified: 2024/03/08 16:15 by z0hpvk