MonsterRocks (10/21/2010)
then how to convert a varchar type variable into datetime type?..
Look up CONVERT in BOL, and choose the 'Style' which matches your text date. 106 looks promising even though the date part delimiter is different:
SELECT CONVERT(DATETIME, '10-jun-2010', 106)
Check that this works against your data, then create a sample data script something like this:
CREATE TABLE #table1 (ID INT, meat VARCHAR(25))
INSERT INTO #table1 (ID, meat)
SELECT 1, 'beef pork' UNION ALL
SELECT 2, 'pork chicken' UNION ALL
SELECT 3, 'pork chicken beef'
and post it here for reference.
Your query will look something like this:
SELECT Fromdate, Todate,
Errors = CASE WHEN (Fromdate >= @pd_from_date and Fromdate <= @pd_to_date)
AND (Todate >= @pd_from_date and Todate <= @pd_to_date) THEN 1 ELSE 0 END
FROM MyTable
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