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 7019423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:10:52+00:00 2026-05-27T23:10:52+00:00

Is it possible to use a comparison operator in a CONVERT or CAST function?

  • 0

Is it possible to use a comparison operator in a CONVERT or CAST function?

I’ve got a statement that looks like this:

SELECT
    ...
    CASE field
        WHEN 'Y' THEN 1  # If Y then True
        ELSE 0           # Anything else is False
    END
    ...
FROM ...

A similar thing happens for a few fields, so I would like to change it to a shorter version:

SELECT
    ...
    CONVERT(BIT, field = 'Y')
    ...
FROM ...

But MSSQL is giving an error Incorrect syntax near '='.

My interpretation of the help is that it should work:

  • CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
  • expression: expression { binary_operator } expression
  • binary_operator: Is an operator that defines the way two expressions are combined to yield a single result. binary_operator can be an arithmetic operator, the assignment operator (=), a bitwise operator, a comparison operator, a logical operator, the string concatenation operator (+), or a unary operator.
  • comparison operator: ( = | > | < | >= | <= | <> | != | !< | !> )

I ran a few tests and got these results:

SELECT CONVERT(BIT, 0)       // 0
SELECT CONVERT(BIT, 1)       // 1
SELECT CONVERT(BIT, 1+2)     // 1
SELECT CONVERT(BIT, 1=2)     // Error
SELECT CONVERT(BIT, (1=2))   // Error
SELECT CONVERT(BIT, (1)=(2)) // Error
  • 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-05-27T23:10:52+00:00Added an answer on May 27, 2026 at 11:10 pm

    I think you are misinterpreting the documentation for CONVERT. There is nothing in the documentation for CONVERT that states it will handle an expression that makes use of the comparison operators, only that it accepts an expression. It turns out that CONVERT does not handle every valid SQL expression. At the very least it cannot handle the results of an expression that uses a comparison operator.

    If you check the documentation for Operators, you’ll see that the comparison operators (which is what you want = to be, in this case) return a Boolean data type, and are used in WHERE clauses and control-of-flow statements. From the documentation for Operators:

    The result of a comparison operator has the Boolean data type, which has three values:
    TRUE, FALSE, and UNKNOWN. Expressions that return a Boolean data type are known as
    Boolean expressions.

    Unlike other SQL Server data types, a Boolean data type cannot be
    specified as the data type of a table column or variable, and cannot
    be returned in a result set.

    …

    Expressions with Boolean data types are used in the WHERE clause to
    filter the rows that qualify for the search conditions and in
    control-of-flow language statements such as IF and WHILE…

    That helps to explain why SQL like SELECT 1=2 is invalid SQL, because it would create a result set with a Boolean data type, which the documentation says is not allowed. That also explains why the CASE WHEN construct is necessary, because it can evaluate the comparison operators and return a single value of a data type that SQL Server can return in a result set.

    Furthermore, if you look at the documentation for CONVERT, you’ll see that Boolean is not supported in either CAST or CONVERT (see the table towards the middle of the page, there is no Boolean data type in there).

    For your purposes, I think you’re stuck using CASE WHEN. If it helps you can write it all on one line:

    CASE WHEN field = 'Y' THEN 1 ELSE 0 END
    

    Alternatively, you could create a UDF to handle the CASE expression (something like dbo.func_DoCase(field, 'Y', 1, 0)), but personally I would just stick with CASE WHEN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make '==' operator use approximate comparison in my program: float values
One of my dreams is to use python rich comparison (something like __eq__ )
Possible Duplicate: Case insensitive string comparison in C++ C++ count and map How to
Possible Duplicates: Using == or Equals for string comparison Are string.Equals() and == operator
Possible Duplicate: String comparison in Python: is vs. == When is the == operator
If I use something like [ntext2] <> '1,032.5', I get this error: The data
I want to do a 'select' in MySQL using the operator 'LIKE'. But I
Is it possible to use the operators defined in glm::gtx::comparison in stl algorithms? Specifically
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
I have recently started looking into Google Charts API for possible use within the

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.