SQLTeam.com | Weblogs | Forums

Count is not working

I have a column with mix of blank cells, negative numbers in decimals, and positive numbers in decimals.

I am trying to perform 2 counts. One to count the numbers in negative i.e. less than 0 and the other is to count anything 0 or above (positive numbers)

but I keep getting error message saying unable to convert to varchar value to data type int.

any advise?


SELECT 
    SUM(CASE WHEN TRY_CAST(column_name AS decimal(19, 2)) >= 0.0 THEN 1 ELSE 0 END) AS count_pos_values,
    SUM(CASE WHEN TRY_CAST(column_name AS decimal(19, 2)) <  0.0 THEN 1 ELSE 0 END) AS count_neg_values
FROM dbo.your_table_name

Thank you so much. It worked but giving me 0 :frowning: and I am not sure why

I'd need some sample data to figure out what's going on.