openlang

Comparing an NTEXT field against a fixed value without type errors

When I try an expression like

[longtext3] <> '2,048.6',

the following error appears:

The data types ntext and varchar are incompatible in the not equal to operator.

Ideally, comparisons would work identically regardless of the column's type. (The <> operator works with both INT and NVARCHAR).

The conversation continues 2

Follow the reasoning
  1. Permalink

    The ntext type has been deprecated, with nvarchar(max) as its replacement. Changing the table's column type would be the preferable fix, if that's possible. Comparing against a varchar literal then causes no difficulty.

    Failing that, you'll need a cast of the value ahead of the comparison:

    cast([longtext3] as nvarchar(max))
      <> '2,048.6'

    Another thing worth trying is an nvarchar literal, which addresses certain related type issues:

    cast([longtext3] as nvarchar(max))
      <> N'2,048.6'

Other sides of the same question

  1. Missing field through a derived table in SQL

    I'm executing the following SQL statement: It produces the following error: #1054 - Unknown column 'shipping.post_id' in 'on…

    Replies 2Latest activity sql
  2. Testing D arrays for a matching entry

    with associative arrays this is possible how is this done with ordinary arrays? I'd like a validation expression, for example

    Replies 2Latest activity d
  3. Removing an optional column safely with PostgreSQL 9+

    I want to remove a column belonging to a table. What's the way to determine whether that column is present? I read the manual…

    Replies 2Latest activity sql