Question

I need to create an anniversary report. This is the original code;

SELECT  cs.id AS csid ,
        csi.Search_Number AS searchID ,
        csi.date_of_placement AS datePlaced ,
        ui.firstname ,
        ui.lastname ,
        cp.name AS companyname ,
        cp.address_line_1 ,
        cp.address_line_2 ,
        cp.city ,
        cp.state ,
        cp.zip ,
        cp2.name AS currentcompanyname ,
        cp2.address_line_1 AS current_address_line_1 ,
        cp2.address_line_2 AS current_address_line_2 ,
        cp2.city AS current_city ,
        cp2.state AS current_state ,
        cp2.zip AS current_zip
FROM    client_searches_individuals AS csi
        LEFT JOIN users_info AS ui ON ui.id = csi.individual_number
        LEFT JOIN client_searches AS cs ON cs.id = csi.search_number
        LEFT JOIN companies AS Cp ON cp.id = cs.company_number
        LEFT JOIN companies AS cp2 ON cp2.id = ui.current_company_number
WHERE   0 = 0 
            <!--- This is for the varchar date field, take away anything with the year selected --->
                and csi.date_of_placement not like '#dateformat(attributes.startdate,'yyyy')#%'

            <cfif attributes.startdate neq "">
                and right(csi.date_of_placement,5) >= '#dateformat(attributes.startdate,'mm/dd')#'
            </cfif>
            <cfif attributes.enddate neq "">
                and right(csi.date_of_placement,5) <= '#dateformat(attributes.enddate,'mm/dd')#'
            </cfif>
        AND ISNULL(cs.id, 0) > 0
ORDER BY date_of_placement

It's poor code because running it as of 2013/12/31 will give me anniversaries for years not = 2013 and includes rows from 2014.

I've tried using datediff but I'm getting invalid date errors for null or empty date_of_placement (varchar) column.

All I really need is any record that has a placement date older than one year based on today's date.

Using the following code;

WHERE   0 = 0
        AND ( DATEDIFF(year, CAST(csi.date_of_placement AS DATETIME), GETDATE()) ) >= 1
        AND ISNULL(cs.id, 0) > 0
        AND ISNULL(date_of_placement, '') > ''

I get;

Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

  • SQL Server 2005
  • CF 9

I did a query to correct date_of_placement. Then a second query or the first query to select anything a year old or older.

SELECT  *
FROM    placedIndGoodDates
WHERE   0 = 0
        AND DATEDIFF(year, datePlaced, '#asofdate#') >= 1
ORDER BY date_of_placement

Query Of Queries syntax error.

Encountered "DATEDIFF ( year. Incorrect conditional expression, Expected one of [like|null|between|in|comparison] condition,

I'm missing something fundemental.

No correct solution

OTHER TIPS

Consider doing a sub-select to format your date_of_placement as a DATETIME, and work out what you want to do with records where date_of_placement is null or empty.

You could exclude those rows using a WHERE clause, or give them some arbitrary date in the future, depending on your needs.

Once that is done you can use date range comparisons without issue, instead of manually comparing to the year etc and running into boundary issues.

UPDATE:

Based on the extra info, use the WHERE clause to remove invalid entries before feeding those values into DATEDIFF, e.g.

SELECT
    CSI.OriginalDate,
    DATEDIFF(year, CSI.OriginalDate, GETDATE()) AS YearBoundariesCrossed,
    DATEDIFF(month, CSI.OriginalDate, GETDATE()) / 12 AS KindaFullYearsDifference
FROM (
    -- sub-select to remove invalid dates and convert to date
    SELECT
        CAST(date_of_placement AS datetime) AS OriginalDate,
        -- specify just the fields you need, or use the lazy *
        *
    FROM client_searches_individuals
    -- use whatever check(s) you need here, I like to check for LEN() > 0
    -- NULL entries fail this test too, which is good
    WHERE LEN(date_of_placement) > 0
) CSI
-- could join "CSI" to your other tables here, not doing that for this sample

-- normal date comparison OK here
WHERE CSI.OriginalDate < GETDATE()

For extra points I've included the difference between DATEDIFF(year, ..) and DATEDIFF(month, ..) / 12. The latter is better if you want at least 12 month boundaries crossed.

NOTE: DATEDIFF counts just that, boundaries crossed (2013-12-31 vs 2014-01-01 = 1 when using 'year' or 'month' or 'day' etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top