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
 SQL Server Development (2000)
 Problems with CASE

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-11-13 : 19:25:11
Magnus writes "Hi!!

I am trying to run the this query but it doesn't run for some values but it does for some...

DECLARE @ORDERBY TINYINT
SELECT @ORDERBY = 3

SELECT *, NumberOfRecords = (SELECT COUNT(1) FROM Temp)
FROM Temp
ORDER BY CASE WHEN @ORDERBY = 1 THEN TitlesName
WHEN @ORDERBY = 2 THEN Language
WHEN @ORDERBY = 3 THEN Username
WHEN @ORDERBY = 4 THEN DownloadDate
WHEN @ORDERBY = 5 THEN Grade
ELSE Grade
END

If I have @ORDERBY less than 4 the following error is displayed:
Server: Msg 241, Level 16, State 1, Line 4
Syntax error converting datetime from character string.

If it's bigger or equal to 4 the query runs perfectly ?!?!?!

Titlesname, Language, username is VARCHAR()
DownloadDate is DateTime
Grade is TINYINT

I can't figure out what is wrong, I use SQL Server 2000 with SP1 and Win2000 SP2

Thanks
- Magnus"
   

- Advertisement -