Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update casts.sql #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update casts.sql
Add BOOLEAN TO BIT, INT4 TO BIT, BIT TO INT4
  • Loading branch information
hmdgirl authored Feb 6, 2023
commit b80dc7dcab12b27018d336440fc208d07eb6653c
18 changes: 18 additions & 0 deletions sql_bits/casts.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
-- BOOLEAN TO BIT
CREATE OR REPLACE FUNCTION _boolean_to_bit(BOOLEAN)
RETURNS bit AS $$
SELECT
case $1 when true then 1::bit
else 0::bit end
$$ IMMUTABLE STRICT LANGUAGE SQL;

CREATE CAST (BOOLEAN AS bit)
WITH FUNCTION _boolean_to_bit(BOOLEAN)
AS IMPLICIT;

-- INT4 TO BIT(cast from type integer to type bit already exists)
-- select castsource::regtype,casttarget::regtype,castcontext,castfunc from pg_cast where castsource='integer'::regtype and casttarget='bit'::regtype;
update pg_cast set castcontext='i' where castsource ='integer'::regtype and casttarget='bit'::regtype;

-- BIT TO INT4(cast from type bit to type integer already exists)
-- select castsource::regtype,casttarget::regtype,castcontext,castfunc from pg_cast where castsource='bit'::regtype and casttarget='integer'::regtype;
update pg_cast set castcontext='i' where castsource ='bit'::regtype and casttarget='integer'::regtype;

-- TIME TO INTEGER
CREATE OR REPLACE FUNCTION _time_to_integer(time with time zone)
Expand Down