I believe the simplest way is to use the correct format of type date
( here has a list of codes and usable formats for dates):
SELECT NOME_CLIENTE,
COUNT(NUMERO_BILHETE) AS 'QUANTIDADE DE BILHETES'
FROM BANCO.DBO.CLIENTES
WHERE CONVERT(nvarchar(6), DATA_EMBARQUE, 112) = '2017-01' --substituir por @ano_mes_param
GROUP BY NOME_CLIENTE
Another alternative would be to get the first positions of the date for comparison using the LEFT()
:
WHERE LEFT(CONVERT(varchar, DATA_EMBARQUE, 112), 6)
EDITED
After commenting on this answer () "Encapsulating column with function is not a good practice because it makes the code non sargable." See article #, I believe the best solution would be as follows (taking into account do not use functions in the where ):
DECLARE @DATA_INI DATE, @DATA_FIM DATE
SELECT @DATA_INI = '2018-01-01'
SELECT @DATA_FIM = EOMONTH(@DATA_INI)
SELECT NOME_CLIENTE,
COUNT(NUMERO_BILHETE) AS 'QUANTIDADE DE BILHETES'
FROM BANCO.DBO.CLIENTES
WHERE DATA_EMBARQUE >= @DATA_INI
AND DATA_EMBARQUE <= @DATA_FIM
GROUP BY NOME_CLIENTE
I used EOMONTH () to be able to pick up the last day of the month; but you can also pick up the 1 day of the next month and check if DATA_EMBARQUE < @DATA_FIM
(for this you would use DATEADD(month, 1, DATA_EMBARQUE)
).