Home Forums SQL Server 2008 T-SQL (SS2K8) How to use NOT IN or Not Exist custom in Datetime? RE: How to use NOT IN or Not Exist custom in Datetime?

  • Hope that following should help you:

    SELECT tf.EmpNo, tf.ChkDate

    FROM filesTA tf

    WHERE tf.ChkDate BETWEEN '20121001' AND '20121005'

    AND NOT EXISTS (SELECT 1 FROM SalaryDay2 sd2

    WHERE sd2.sEmpNo = tf.EmpNo

    AND sd2.sDate = tf.ChkDate)

    Please note:

    1. Never convert datetime to anything else (varchar or int) for datetime comparison.

    If your SalaryDay2.sDate is varchar, convert it to datetime instead (or it will be properly implicitly converted if it's in ISO)

    2. Note the format I've used - it's one of possible ISO variations: Year then Month then Day. You can use '1 Oct 2012', it will also work perfectly as month is explicitly known.

    3. Used BETWEEN will only work properly if your datetime values do not contain time part. Otherwise, you should use greater-than-or-equal-to and less-than in WHERE clause for datetime filtering:

    WHERE tf.ChkDate >= '20121001' AND tf.ChkDate < '20121006' --note the "to" boundary...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]