Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 problem with CAST function

Author  Topic 

hitman
Starting Member

23 Posts

Posted - 2006-08-01 : 06:40:13
Hi guys,

I have a problem with CAST function.
The query looks like this:

SELECT (CASE q.[answer_type] WHEN 2 THEN CAST(u.[content] AS VARCHAR) ELSE CAST(1 AS BIT) END) AS [selected]
FROM UserAnswers u, Question q
WHERE u.[user_id] = 2 AND u.[question_id] = q.[question_id]

Column u.[content] is varchar(max). So the idea is whether the type of the question is 2 return varchar else return true. But this query every time returns bit (e.g. content is 123 and is converted to 1).

Thanks for help

nr
SQLTeam MVY

12543 Posts

Posted - 2006-08-01 : 07:00:00
A case statement can only return one datatype - think bit has higher precedence than varchar so your value will be converted to a bit whatever the case expression returns.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

hitman
Starting Member

23 Posts

Posted - 2006-08-01 : 07:36:55
ok, thank you very much.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-01 : 11:20:58
Note that every non zero(except NULL) will be converted to 1 in bit datatype

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -