首页 > 解决方案 > How to filter integer with specific numbers in sql

问题描述

So in one of my assignment, they wanted me to sort out numbers that ends with "00" for the queries. This means no "00" on middle, just at the end.

For example: 100, 200, 300, etc. This is how I want it to come out.

The problem is, I have no idea how to do it. I have try to convert it to string with CASE and CONVERT, but it gives me error instead. Do I convert it first on Select then sort it later on, or should it all happen at the same time?

Or do you have any better suggestion?

This is some of my attempts at doing it.

SELECT name, SUM(count) AS freq
FROM [baby.baby_names]
WHERE gender = 'M' AND year < 2010 AND freq like '%[00]'
GROUP BY name
ORDER BY freq DESC;

Thank you for your time.

标签: sqlcase

解决方案


Check if a number ends with two zeros is equivalent to checking if it is a multiple of 100. So you could just use the modulo operator:

where freq % 100 = 0

Some databases dot not support % and want mod() instead:

where mod(freq, 100) = 0

推荐阅读