Using a DATE custom field in an SQL statement

I went into Set Up IM Options -> Miscellaneous tab -> Custom Fields and added an optional custom field called SDSDate and set it to be a Data Type of Date.

Then in Maintain Items I set an SDSDate value for some items.

These custom field values are obviously stored in timItem.  There can be up to six custom field values set up, and all are stored in timItem as varchar(15).  Even if a custom field is set up as a "Date" Data Type, it is stored as a varchar in timItem.

I created a copy of vdvShipmentLines and I'm trying to modify this copy of the view so that I can use UserFld1 in a comparison.  I added timItem_1.UserFld1 to the view by simply checking the checkbox for that field.  Then in the text of the view I changed "timItem_1.UserFld1" to "CONVERT(Date, timItem_1.UserFld1) AS SDSDate."  That's all basic stuff.  Having done that, I would think I should be able to do something like this in a query.

SELECT * from vdvShipmentLine_custom
WHERE SDSDate IS NOT NULL AND ShipDate > SDSDate

However, that fails and returns:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

Is this because some item records contain NULLs in UserFld1, and a date comparison obviously can't be done on a NULL value?  That is why I included "IS NOT NULL," but maybe that approach doesn't work.  I found a forum comment saying that even if you exclude records with NULL values in the WHERE clause PRIOR to attempting the date comparison/calculation, the NULL values in the table will still cause it to fail.  I don't know if that's true, but it seems plausible.

I have tried using both CAST and CONVERT in the view without success.

Has anyone ever added a Date custom field in Maintain Items and then used that custom field in a comparison or calculation in an SQL statement?

Using version 7.70.2 and SQL 2008 R2.

Thanks for any help.

  • 0
    Maybe these will help you understand some of what goes on with dates. Notice I defined the @DateDate with a time component also. If you set it back to 00:00:00.00 it will act a little different, but also note that you can get just the date component of this with CONVERT(DATE... also, comparing two NULLs as = doesn't work, you have to separately look at each to see if they are NULL (unless they are the same data type? - I didn't look at that, but it's a simple test).
    Anyway, hopefully some of this will help.

    DECLARE @NullVarChar varchar(10), @DateVarChar Varchar(10), @NullDate datetime, @DateDate datetime

    SELECT @NullVarChar = NULL, @DateVarChar = '05/31/2017', @NullDate = NULL, @DateDate = '05/31/2017 00:01:00.00'

    SELECT 'WHERE is true' WHERE @NullVarChar IS NULL
    SELECT 'WHERE is true' WHERE CONVERT(DATETIME,@DateVarChar) = @DateDate
    SELECT 'WHERE is true' WHERE CONVERT(DATETIME,@DateVarChar) = CONVERT(DATE, @DateDate)
    SELECT CONVERT(varchar(10), @DateDate,101), CONVERT(DATE, @DateDate), CONVERT(SMALLDATETIME, @DateDate)
    SELECT 'WHERE is true' WHERE @NullVarChar = @NullDate
    SELECT 'WHERE is true' WHERE @NullVarChar IS NULL AND @NullDate IS NULL


    Darrick
  • 0 in reply to dbcoles
    Ralph: I think the issue is that your WHERE clause is still evaluating the VarChar field. If you try "WHERE timItem_1.UserFld1 IS NOT NULL AND timItem_1.UserFld1 <> '' AND ShipDate > CONVERT(Date, timItem_1.UserFld1)" I believe you'll get the results you're looking for.
  • 0 in reply to D.Hart
    Thanks a lot for the replies, guys. I was not able to make this work in SQL SMS. However, I was able to do it pretty easily in the Sage Intelligence Connector tool, which is where I was heading with this SQL view anyway.
    In the SI Connector tool, I added the SQL expression CASE WHEN ShipDate <= UserFld1 THEN 'YES' ELSE 'NO' END, and that works fine. It doesn't even complain if the SDSDate field is blank.