declare @date1 datetime, @date2 datetime;
set @date1 = '01/21/2010';
set @date2 = '01/22/2010';
SELECT
DISTINCT count(*) AS Form3and4,
br.br_districtid
FROM [192.168.3.201].[fdhs_server].TELEMONITORING.beneficiaryregistration br
INNER JOIN [192.168.3.201].[fdhs_server].TELEMONITORING.beneficiaryvisitrecords bvr
ON bvr.vr_beneficiaryid = br.br_beneficiaryId
WHERE charindex('F3', vr_formsfilled) > 0
OR charindex('F4', vr_formsfilled) > 0
AND convert(varchar, vr_visitDate, 101) BETWEEN @date1 AND @date2
GROUP BY br.br_districtid
-- here if i replace charindex with like can you tell whether my query will correct or not.and also iam
-- executing the query using linked server
-- No. Correct the mistakes in your query first.
-- SELECT DISTINCT count(*) is meaningless in this context, it won't do anything.
-- There is an OR and an AND in your WHERE clause. If this query returns the rows you want, it's by accident, not by design.
-- You probably want your WHERE clause to look like this:
WHERE (charindex('F3', vr_formsfilled) > 0
OR charindex('F4', vr_formsfilled) > 0)
AND convert(varchar, vr_visitDate, 101) BETWEEN @date1 AND @date2
-- Why are you converting vr_visitDate, which appears to be a date column, into a varchar
-- before comparing it to your start and end dates, which are datetime? Why not manipulate
-- @date1 AND @date2 so that they can be compared directly to vr_visitDate?
-- Questions:
-- Where in the column vr_formsfilled are the strings F3 and F4 likely to be located?
-- Right at the beginning? Right at the end? Somewhere in the middle?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden