Thread: convert integer to bool implicitly
How easy would it be to write a small type extension to have integer automatically convert to bool? For example, I want an implicit conversion that 0 is false and everything else is true. Is this C programming or can you do it with a local procedural language? Thanks Sim
Sim Zacks <sim@compulab.co.il> schrieb: > How easy would it be to write a small type extension to have integer > automatically convert to bool? > For example, I want an implicit conversion that 0 is false and everything > else is true. test=# \d foo; Table "public.foo" Column | Type | Modifiers --------+---------+----------- val | integer | test=# select * from foo; val ----- 0 1 2 3 (4 rows) test=# select val::bool from foo; val ----- f t t t (4 rows) > Is this C programming or can you do it with a local procedural language? You can use regular casts. HTH, Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
I know I can do it explicitly, I need to do it implicitly. I am having a problem with a Microsoft Access front-end , that uses -1 as true and 0 as false. I have everything worked around all the places where I could find it by using a cbool(), but this doesn't work with filters, which probably call a "select where field =-1 or =0." automatically by access. I get an ODBC error saying that "Operator does not exist: boolean=integer. I would like to create the implicit conversion, so that it works both without the cbool from now on, and that filters should work fine. I have tried a number of different ODBC settings, and no matter what setting there is always one problem or another that has to be overcome. I can get it all to work if I put a Before Insert and Update trigger and change the -1 to true, but I would have to do that for every boolean field in my application. It would be a lot easier and make sense system-wise to implicitly convert the integer to bool. Andreas Kretschmer wrote: > Sim Zacks <sim@compulab.co.il> schrieb: > >> How easy would it be to write a small type extension to have integer >> automatically convert to bool? >> For example, I want an implicit conversion that 0 is false and everything >> else is true. > > test=# \d foo; > Table "public.foo" > Column | Type | Modifiers > --------+---------+----------- > val | integer | > > test=# select * from foo; > val > ----- > 0 > 1 > 2 > 3 > (4 rows) > > test=# select val::bool from foo; > val > ----- > f > t > t > t > (4 rows) > > >> Is this C programming or can you do it with a local procedural language? > > You can use regular casts. > > > HTH, Andreas
I got it working. This has been bothering me for a long time, but now that it was listed as an official bug in my system, I had to deal with it. I'll post the solution under a new post, because I think this will help a lot of people. Sim Zacks wrote: > I know I can do it explicitly, I need to do it implicitly. > I am having a problem with a Microsoft Access front-end , that uses -1 > as true and 0 as false. > I have everything worked around all the places where I could find it by > using a cbool(), but this doesn't work with filters, which probably call > a "select where field =-1 or =0." automatically by access. > > I get an ODBC error saying that "Operator does not exist: boolean=integer. > > I would like to create the implicit conversion, so that it works both > without the cbool from now on, and that filters should work fine. > I have tried a number of different ODBC settings, and no matter what > setting there is always one problem or another that has to be overcome. > I can get it all to work if I put a Before Insert and Update trigger and > change the -1 to true, but I would have to do that for every boolean > field in my application. It would be a lot easier and make sense > system-wise to implicitly convert the integer to bool. > > Andreas Kretschmer wrote: >> Sim Zacks <sim@compulab.co.il> schrieb: >> >>> How easy would it be to write a small type extension to have integer >>> automatically convert to bool? >>> For example, I want an implicit conversion that 0 is false and >>> everything else is true. >> >> test=# \d foo; >> Table "public.foo" >> Column | Type | Modifiers >> --------+---------+----------- >> val | integer | >> >> test=# select * from foo; >> val >> ----- >> 0 >> 1 >> 2 >> 3 >> (4 rows) >> >> test=# select val::bool from foo; >> val >> ----- >> f >> t >> t >> t >> (4 rows) >> >> >>> Is this C programming or can you do it with a local procedural language? >> >> You can use regular casts. >> >> >> HTH, Andreas