Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

ODBC-compliance; Boolean expressions (Complex criteria in column-expressions a problem?)

$
0
0

Hi there!

I'm almost certain that I've hit upon a couple of faults in SQL-server's ODBC compliance, but before i shoot off a bug report (I've heard that programmers take bug reports as personal insults, and make lifelong enemies out of the bearers of bad news), I wouldn't mind if somebody could point at some fault of mine instead, by pointing out some error in the following (simplified further down) ODBC query.

SELECT Activities.ID,   {fn SUBSTRING(  '[' + RIGHT('0' + { fn CONVERT( { fn DAYOFMONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '/' + RIGHT('0' + { fn CONVERT( { fn MONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '] ',   1,   (CASE WHEN  (CASE WHEN [Activities].[StartingTime] IS NOT NULL THEN -1 ELSE 0 END)AND {fn CONVERT( {fn CONVERT( {fn YEAR( {fn CONVERT([Activities].[EndingTime], SQL_DATE)} )},  SQL_VARCHAR)} + '/' + {fn CONVERT( {fn MONTH({fn CONVERT([Activities].[EndingTime], SQL_DATE)} )},  SQL_VARCHAR)} + '/' + {fn CONVERT( {fn DAYOFMONTH({fn CONVERT([Activities].[EndingTime], SQL_DATE)} )},  SQL_VARCHAR)},  SQL_DATE)}<>{fn CONVERT( {fn CONVERT( {fn YEAR( {fn CONVERT([Activities].[StartingTime], SQL_DATE)} )},  SQL_VARCHAR)} + '/' + {fn CONVERT( {fn MONTH({fn CONVERT([Activities].[StartingTime], SQL_DATE)} )},  SQL_VARCHAR)} + '/' + {fn CONVERT( {fn DAYOFMONTH({fn CONVERT([Activities].[StartingTime], SQL_DATE)} )},  SQL_VARCHAR)},  SQL_DATE)}  THEN  10000  ELSE   0  END) )}  FROM Activities

A conversion module has produced the query from the following short Jet query(-extract):

SELECT Activities.ID,   Mid( "[" & Format([Activities].[EndingTime], "dd\/mm") & "] ", 1,  Iif( [Activities].[StartingTime] IS NOT NULL AND Datevalue([Activities].[EndingTime])<>Datevalue([Activities].[StartingTime]), 10000,  0 ) ) FROM Activities

The ODBC query yields the following error:

"ERROR [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The data types int and datetime are incompatible in the '|' operator."

The highlighted fragment, a single inequality test, appears then to be the stumbling block, as when it's replaced with the simpler test below, the query runs just fine. 

SELECT Activities.ID,   {fn SUBSTRING(  '[' + RIGHT('0' + { fn CONVERT( { fn DAYOFMONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '/' + RIGHT('0' + { fn CONVERT( { fn MONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '] ',   1,   (CASE WHEN  (CASE WHEN [Activities].[StartingTime] IS NOT NULL THEN -1 ELSE 0 END) |1<>1  THEN  10000  ELSE   0  END) )}  FROM Activities

If the pipe-operator is replaced with an AND-operator, however, the query results in the following error:

"ERROR [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]An expression of non-boolean type specified in a context where a condition is expected, near 'AND'."

But if 'AND 1<>1' is removed, leaving one and one only condition in the relevant CASE-statement, the query runs fine again, suggesting that SQL-server cannot handle complex criteria (in columns; in WHERE-clauses there is of course no problem).

_______

The incompatibility of datetimes with the pipe-operator suggests a (severe) limitation of some parser, but as this limitation is stated in clear text, the limitation can presumambly not be dealt with.

Regarding the error with the attempted bypass, i.e. the non-boolean interpretation of what  clearly is (or should be?) a boolean element, can someone shed light on that issue?

The query to be considered, then:

SELECT Activities.ID,   {fn SUBSTRING(  '[' + RIGHT('0' + { fn CONVERT( { fn DAYOFMONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '/' + RIGHT('0' + { fn CONVERT( { fn MONTH( [Activities].[EndingTime])}, SQL_VARCHAR )}, 2) + '] ',   1,   (CASE WHEN  (CASE WHEN [Activities].[StartingTime] IS NOT NULL THEN -1 ELSE 0 END)AND 1<>1  THEN  10000  ELSE   0  END) )}  FROM Activities

Notes to be observed:

Bracketing sub expressions does not help.

The troubles are delivered by a 2008 SQL server.

Many thanks for any reply!


Viewing all articles
Browse latest Browse all 12963

Trending Articles