Question

I am in the process of converting some Access queries into tsql and this error pops up when I try to execute the query. I am guessing that SQL Server does not allow adding of 'bit' types, so I found the cultprit lines where that occurs and they are:

SELECT DISTINCT 
   [modules].[b]+[modules].[w]+[modules].[e]+[modules].[j]+
   [modules].[p]+[modules].[s] AS approvalRating, -- other columns here --

And:

ORDER BY [modules].[b]+[modules].[w]+[modules].[e]+[modules].[j]+
   [modules].[p]+[modules].[s],
   modulePriority.configPriority, 
   [modulePositionalData].[highPos]-[modulePositionalData].[lowPos]+1, 
   modulePositionalData.iMax;

Now I did not create the original Access queries and I have no idea what was the intention of adding that Boolean fields which makes it a bit problematic, but is there a way to allow the operations to be performed and result to be the same as performed by Access?

Was it helpful?

Solution

you can't add bit fields in sql server

But you can cast them to int

so

CAST(yourBitField as int) + CAST (yourSecondBitField as int)

By the way (would say it's due to type preference order), it will be ok if you cast only one of them

CAST(yourBitField as int) + yourSecondBitField

But casting all is probably "easier to read and understand".

OTHER TIPS

You cannot add bits in SQL Server, as they can only be 0 or 1. You can however convert them to integer values first and then add them. To convert a bit to an int, use the CAST function:

CAST([modules].[b] AS INT) + CAST([modules].[w] AS INT) + CAST([modules].[e] AS INT) ... etc

See more on SQL Server type conversion here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

if the fields are fromt typ char it works. SELECT [field1]+ [field2] as test , [field3] FROM [table] order by test

if the fiels are from an other type you must cast them

This works in my database

As they are bits you can 'OR' them together with | : https://msdn.microsoft.com/en-us/library/ms176122.aspx This will give you the same result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top