1

The following query is returning the aforesaid error. The column WNT.USER.PriviligeLevel_SV contains values 1,2,3 or different errors. I need to have the query return 'Administrator' if the value is 1, 'Guest' if the value is 2 and 'User' if the value is 3. Otherwise this column should contain the verbatim error message. I have tried to use ELSE CAST([Wnt.User.PrivilegeLevel_SV] AS nvarchar) and WHEN [Wnt.User.PrivilegeLevel_SV] NOT LIKE '%[^0-9]%' but neither is working. Please advice.

SELECT [Wnt.User.Host] AS 'Machine Name',
         "Account Privilege Level" =
         CASE 
              When [Wnt.User.PrivilegeLevel_SV] = 1 Then 'Administrator'
              When [Wnt.User.PrivilegeLevel_SV] = 2 Then 'User'
              When [Wnt.User.PrivilegeLevel_SV] = 3  Then 'Guest'
              Else [Wnt.User.PrivilegeLevel_SV]
              End 
  FROM [Prod].[AdHoc].[TestBuffer]

SQL Version is 2008 and the WNT.USER.PriviligeLevel_SV column is defined as nvarchar(max)

2 Answers 2

4

You cannot have different datatypes returned by different branches of the same CASE in SQL Server. (This is also true even if a branch will not get executed)

Due to data type precedence rules of SQL Server the VARCHAR values get forced into INT which fails.

You should cast your value from the Else-Branch to NVARCHAR (or VARCHAR) to have all branches of the CASE return the same datatype.

I.e. Else CAST([Wnt.User.PrivilegeLevel_SV] AS NVARCHAR(MAX)) for example.

0
0

Actually it's pretty simple:

SELECT [Wnt.User.Host] AS 'Machine Name',
     "Account Privilege Level" =
     CASE 
          When [Wnt.User.PrivilegeLevel_SV] = '1' Then 'Administrator'
          When [Wnt.User.PrivilegeLevel_SV] = '2' Then 'User'
          When [Wnt.User.PrivilegeLevel_SV] = '3'  Then 'Guest'
          Else [Wnt.User.PrivilegeLevel_SV]
          End 
FROM [Prod].[AdHoc].[TestBuffer]

You need to make sure you enclose numbers in single quotes if your column type is varchar.

5
  • No, the problem is not the values evaluated by, but the values returned from the case statement. Sep 12, 2014 at 13:06
  • A case statement will search the whole table for a match, so when the comparison of an int and a varchar arises, an error is thrown. There's nothing wrong with the values returned by, since these are all string types.
    – John Bell
    Sep 12, 2014 at 13:09
  • No actually SQL Server is just fine comparing varchar to ints as long as they are parsable. Consider this and that for example. (this fails due to different return types while that succeeds) Sep 12, 2014 at 13:13
  • We must be using two completely different languages then, because implicit comparisons between ints and varchars has always thrown an error for me. "That" isn't working by the way.
    – John Bell
    Sep 12, 2014 at 13:18
  • Oh sorry, must have mixed up links: this should fail and that should not. Sep 12, 2014 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.