You may optionally enter a comparison operator (<, <=, >, >=, <> or =) at the beginning of each of your search values to specify how the comparison should be done.
CREATE TABLE new_table AS SELECT * FROM old_table
-- this action can't be done cross databases
147
PL/pgSQL
Datum, Zeit
Jahr aus Datum extrahieren
select extract(year from now())
148
PL/pgSQL
Strings
Strings miteinander verbinden
-- Bei PL/pgSQL sollte man die Funktion concat verwenden.
-- Felder verbinden mit || kann zu NULL im Ergebnisfeld führen
SELECT
field_a
,field_b
,NULL as field_c
,concat(field_a, field_b, field_c) as together
FROM
table_1
150
PL/pgSQL
Datenbank
DB-Funktion erstellen
CREATE OR REPLACE FUNCTION whichContinent(country TEXT)
RETURNS character varying AS $BODY$
DECLARE returnValue character varying;
BEGIN
SELECT INTO returnValue
CASE country
WHEN 'ARGENTINA' THEN 'SOUTH AMERICA'
WHEN 'BELGIUM' THEN 'EUROPE'
WHEN 'BRAZIL' THEN 'SOUTH AMERICA'
WHEN 'CANADA' THEN 'NORTH AMERICA'
WHEN 'DENMARK' THEN 'EUROPE'
WHEN 'FINLAND' THEN 'EUROPE'
WHEN 'FRANCE' THEN 'EUROPE'
ELSE 'UNKNOWN'
END;
RETURN returnValue;
END;
$BODY$ LANGUAGE PLPGSQL;
152
PL/pgSQL
Datenbank
Automatisch hochzählendes Feld
CREATE TABLE myTable
(
id SERIAL,
field1 varchar(100)
)
153
PL/pgSQL
Datum, Zeit
Aktuelles Datum/Uhrzeit
SELECT now();
154
PL/pgSQL
Datenbank
Erstellen und Befüllen Tabelle aus Query
DROP TABLE myTable;
CREATE TABLE myTable AS SELECT * FROM mySecondTable;
156
PL/pgSQL
Datum, Zeit
Monat aus Datum extrahieren
-- You get the local name for the spoken month name
SELECT to_char(now(), 'TMMonth')
-- results in german to "Januar" or "Dezember" (= "January", "December")
157
PL/pgSQL
Datum, Zeit
Erster und letzter Tag des Monats ermitteln
select date_trunc('month', now()) -- first day in month
select date_trunc('month', now()) + interval '1 month - 1 day' -- last day in month
181
PL/pgSQL
Datenbank
NULL Wert eines Feldes übersteuern
SELECT COALESCE(MyColumn1, 1.0) AS MyColumnWithoutNULL;