Question : Why am I getting Msg 245 for this:

I caseing this wrong - every time i fire, i get this:

Server: Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'A       ' to a column of data type int.


select CASE
      WHEN liquidityflag IN (17,18,25,26,27,28,43,44) THEN  'A'
      WHEN liquidityflag IN (02,21,22,24) THEN 'R'
      WHEN liquidityflag IN (19,30,35,40,51,53,55) THEN 'X'
      WHEN liquidityflag IN (49,50) THEN 'O'
             ELSE liquidityflag
           END AS LF,  
Destination as EndPoint,+''+RTRIM(LEFT(CONVERT(char(16),CAST(count(*) AS MONEY),1),13)) AS '   #Trades',+''+
RTRIM(LEFT(CONVERT(char(16),CAST(SUM(LastFillQuantity) AS MONEY),1),13)) AS 'Volume    '
FROM trading.dbo.trade
where timeofexecution between dateadd(day,-1, cast(convert(char(10),getdate(),120) + ' 00:00:00' as datetime) )
and dateadd(day,-1, cast(convert(char(10),getdate(),120) + ' 23:59:59' as datetime))      
and destination IN ('IIII','LLLL','DDDD','CCC')
and destination IS NOT NULL
and liquidityflag <> '?'
GROUP BY liquidityflag, destination
ORDER BY liquidityflag, destination

Answer : Why am I getting Msg 245 for this:

I suspect, from your initial error that you actually have 'A      '  etc, rather than 'A', stored in liquidityflag. This will definitely be the case if the datatype is char(6) rather than varchar(6) etc, since it has to be of length 6 (or whatever you have).
Also, your group-by means that you get one row per value of liquidityflag, not one row per unique resultset -
You need to remove any white-space from your data (use rtrim & ltrim to be sure), and then group by hte resulting value.

try something like this :

select CASE
     WHEN liquidityflag IN ('17','18','25','26','27','28','43','44') THEN  'A'
     WHEN liquidityflag IN ('02','21','22','24') THEN 'R'
     WHEN liquidityflag IN ('19','30','35','40','51','53','55') THEN 'X'
     WHEN liquidityflag IN ('49','50') THEN 'O'
     ELSE ltrim(rtrim(liquidityflag)) END AS LF,
--     ELSE cast(liquidityflag as varchar) END AS LF,  
Destination as EndPoint,+''+RTRIM(LEFT(CONVERT(char(16),CAST(count(*) AS MONEY),1),13)) AS '   #Trades',+''+
RTRIM(LEFT(CONVERT(char(16),CAST(SUM(LastFillQuantity) AS MONEY),1),13)) AS 'Volume    '
FROM trading.dbo.trade
where timeofexecution between dateadd(day,-1, cast(convert(char(10),getdate(),120) + ' 00:00:00' as datetime) )
and dateadd(day,-1, cast(convert(char(10),getdate(),120) + ' 23:59:59' as datetime))    
and destination IN ('IIII','LLLL','DDDD','CCC')
and destination IS NOT NULL
and cast(liquidityflag as varchar) <> '?'
GROUP BY destination, CASE
     WHEN liquidityflag IN ('17','18','25','26','27','28','43','44') THEN  'A'
     WHEN liquidityflag IN ('02','21','22','24') THEN 'R'
     WHEN liquidityflag IN ('19','30','35','40','51','53','55') THEN 'X'
     WHEN liquidityflag IN ('49','50') THEN 'O'
     ELSE ltrim(rtrim(liquidityflag)) END
ORDER BY destination, 1

Random Solutions  
 
programming4us programming4us