Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:37:42+00:00 2026-06-05T17:37:42+00:00

I have a parameterized postgresql query that users interact with through a PHP front-end.

  • 0

I have a parameterized postgresql query that users interact with through a PHP front-end. One of the parameters involves a boolean value, and I want to give the user the option of “True”, “False”, “Unknown” (Null) or “Doesn’t matter” (return any result).

Rewriting the actual query isn’t an option (the parameterized queries are stored in a database table, not in the PHP code), so I need to have a boolean “any” value.

Put another way, what value can I assign “:parameter1” in this query to get any row, regardless of the value of “boolean_column”?

SELECT some_column, some_other_column
FROM some_table
WHERE boolean_column = :parameter1

EDIT: I need to clarify — I can rewrite the query, but the program cannot rewrite the query (i.e., I can’t drop or rewrite the where clause in response to user input at runtime).

EDIT2: Here’s my final solution (thanks couling!)

SELECT some_column, some_other_column
FROM some_table
WHERE (:parameter1::text = 'any' OR boolean_column = :parameter1::boolean)

Of course, the program has to restrict possible values to ‘any’ or a string value that can be cast to boolean (e.g., ‘true’, ‘t’, ‘false’, ‘f’).

EDIT3: The previous solution works in PHP with PDO, but does not work with python’s psycopg2 or directly in postgresql for some reason. I’ve tried a variation with a case statement, like so:

SELECT some_column, some_other_column
FROM some_table
WHERE (CASE WHEN :parameter1::text in ('true', 'false') THEN boolean_column = :parameter1::boolean ELSE TRUE END)

But that doesn’t work either. It seems no matter what, I can’t stop postgresql from trying to evaluate “:parameter1::boolean”, even if it’s behind a false WHEN condition. This is a real pickle…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:37:44+00:00Added an answer on June 5, 2026 at 5:37 pm

    Well your query must change. Booleans have two values true and false with nulls never being equal to anything (even null doesn’t equal null).

    You could of course use:

    SELECT some_column, some_other_column
    FROM some_table
    WHERE (boolean_column = :parameter1 OR :parameter1 is null)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that builds dynamic parameterized SQL queries. The following query returns
I have several Jenkins parameterized jobs that uses the same parameters and parameters values.
I have a parameterized SQL query targetted for SQL2005 which is dynamically created in
Normally the parameterized SQL works great for CRUD, but I have one instance where
I have been investigating Table-Valued Parameters in SQL Server 2008, and I've discovered that
I have used parameterized query number of times I know it helps in preventing
I have built a common app that works with PostgreSQL and should work on
I have a parameterized query. Depending on parameter values optimal query plan varies significantly.
I have a generic type that is parameterized on some Enum, declared like this:
I have a crystal report with a bunch of parameterized fields that are being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.