comparison operator in count() is not working [closed]
-
18-02-2021 - |
Question
The query (SELECT 15 > 14 FROM Duel
) Working in MySql, but when I tried in MS-SQL Server getting following error:
SQL Error(102): Incorrect syntax near '>'
I have a requirement where I want to see if at least 1 record exist return 1 or else 0
SELECT count(emp.name) > 0
FROM ****
WHERE *** IN (***)
Check the db fiddle
Solution
You can use IIF to return a 1 or 0 based on a condition, see example:
SELECT IIF(COUNT(EmpID) > 0, 1, 0) AS [RecordsExist]
FROM Employee
WHERE EmpName IN ('Dave', 'Carol')
Returns 1 if any rows in the Employee table exist with an EmpName value of 'Dave' or 'Carol'
OTHER TIPS
I have a requirement where I want to see if at least 1 record exist return 1 or else 0
You can do that with following query in MS-SQL (db-fiddle)
SELECT CASE WHEN count (*) >= 1 THEN 1 ELSE 0 END
FROM new_table_name
Not sure how mySQL treats this SELECT 15 > 14 FROM Duel
, but in MS-SQL it's not complete statement (even columns exists with the name 15 and 14), operators (=
, <>
, !=
, >
, <
, LIKE
, IN
, NOT IN
) usually used in WHERE
and ON
clauses. When it used in SELECT
clause it must complete the Boolean logic (CASE
, IIF
).