1

I have a batch code table that contains codes that I use to label certain date periods. However there are some dates for which there doesn't exist an entry in the management table. For these I want to use a default value in the table that has the year '9999'. However when I try to change the year I get a conversion error.

I'm able to get the correct value from the code bellow, I just can't seem to get it to work in the actual query.

/* Works */
DECLARE @Date date
SET @Date = '2015-09-20'
SELECT CAST('9999' + '-' + CAST(MONTH(@Date) AS varchar) + '-' + CAST(DAY(@Date) AS varchar) AS date)

Conversion failed when converting date and/or time from character string.

   /* Conversion error */ 
     SELECT 
            EndDate,
            CASE 
                WHEN (SELECT BatchCd FROM BatchCodeMGMT WHERE Orders.EXITDATE BETWEEN BatchStartDt AND BatchEndDt AND RegionBInd = 0 ) IS NULL THEN
                    (SELECT BatchCd FROM BatchCodeMGMT WHERE  --We set the year to the default year which is 9999 and compare them going backwards
                        CONVERT(date,('9999' + '-' + 
                                CAST(MONTH(Orders.EndDate) AS varchar) + '-' + 
                                CAST(DAY(Orders.EndDate) AS varchar) 
                        ))
                     BETWEEN BatchStartDt AND BatchEndDt AND AdultEdInd = 0 ) 
                ELSE
                    (SELECT BatchCd FROM BatchCodeMGMT WHERE Orders.EXITDATE BETWEEN BatchStartDt AND BatchEndDt AND AdultEdInd = 0 ) 
            END BatchCd
        FROM Orders 

The batch code table is more or less like this, all of the columns are non-nullble as well. So for a date in Orders of '2013-09-15' I'd like to have it changed to '9999-09-15' and use that to pick out the batch code from the table.

-------------------------------------
|BatchCd | BatchStart | BatchEnd    |
-------------------------------------
|  Pr2   | 9999-01-16 | 9999-06-14  |
|  Pr1   | 9999-06-15 | 9999-01-15  |
|  Pr2   | 2015-01-16 | 2015-06-14  |
|  Pr1   | 2015-06-15 | 2015-08-15  |
-------------------------------------

Edit: I realize there could be potential problems for when there are overlapping dates. However for this table that should not happen.

12
  • Do you have any rows where Order.EndDate is NULL? Oct 12, 2015 at 13:26
  • I agree with the two above. In advance, it's not a good idea to use a "dummy" value on a column to indicate that it doesn't exist. You have to declare it as "with nulls", and insert a NULL value for the dates that don't exist. Oct 12, 2015 at 13:26
  • @TabAlleman No, the column is declared as not null.
    – adc90
    Oct 12, 2015 at 13:33
  • @SergioInternicola I'm not sure what you mean, all the values I'm referring to would have a value. Except for the between clause for dates not in the BatchCodeMGMT table.
    – adc90
    Oct 12, 2015 at 13:33
  • You might have a mistyped record in there. Try running this first select EndDate,ExitDate from Orders where ISDATE(EndDate) = 0 OR ISDATE(ExitDate) = 0 Oct 12, 2015 at 13:44

1 Answer 1

0

You may need to tell the convert command exactly how you formatted the data. It sees ####-##-##, but it doesn't see instructions telling it that it is yyyy-mm-dd. Look at the table here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

That will give you valid date formats. Maybe you will have to change the way you format it or maybe you simply need to tell it what format you are using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.