Showing posts with label database. Show all posts
Showing posts with label database. Show all posts
Friday, October 31, 2014
Postgres get First or Last day of the month
-- Last day of the month
CREATE OR REPLACE FUNCTION public.fn_getlastofmonth (
date
)
RETURNS date AS
$body$
begin
return (to_char(($1 + interval '1 month'),'YYYY-MM') || '-01')::date - 1;
end;
$body$
LANGUAGE 'plpgsql'
IMMUTABLE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;
-- First day of month
CREATE OR REPLACE FUNCTION public.fn_getfirstofmonth (
date
)
RETURNS date AS
$body$
begin
return (date_trunc('MONTH', $1)::DATE);
end;
$body$
LANGUAGE 'plpgsql'
IMMUTABLE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;
Usage:
SELECT fn_getlastofmonth('2014-08-03');
SELECT fn_getfirstofmonth('2014-08-03');
Thursday, August 7, 2014
Tech proverbs #10. On 3nf
Some people hate 3nf and some people love it. 3nf doesnt make it right or wrong. It just breaks or db to modular tables for your system to be flexible in times of change. When a programmer designs an OO, he can design a 3NF db.
Thursday, July 31, 2014
Tech proverbs #1
When dealing with database. Dont insert historical or logs related manually in the code. Create a trigger to insert to a table log when the main table is updated. This is good for transaction histories involving money. #postgres
Subscribe to:
Posts (Atom)