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.
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 qWHERE 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. |
 |
|
hitman
Starting Member
23 Posts |
Posted - 2006-08-01 : 07:36:55
|
ok, thank you very much. |
 |
|
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 datatypeMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|