smalldatetime convert problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcharnigo
    New Member
    • Feb 2007
    • 20

    smalldatetime convert problem

    I am converting a varchar(10) field to a smalldatetime in a stored procedure but have run into a problem. Some of the rows contain an invalid date. varchar(10) format is mm/dd/yyyy some rows are filled with 00/00/0000 however causing the convert to fail. how can I get around this? Updating the table is not an option. My convert is as follows:
    ...
    WHERE CONVERT(datetim e, CIFExpDate) < CURRENT_TIMESTA MP
    ORDER BY CONVERT(datetim e, CIFExpDate) DESC
    ...

    Thanks for any help!
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    use the CASE FUNCTION and the
    use ISDATE FUNCTION


    -- CK

    Comment

    • dcharnigo
      New Member
      • Feb 2007
      • 20

      #3
      Ok Closer I got the select to work:

      Code:
      SELECT
          CIFExpDate = 
      	CASE
      		WHEN ISDATE(CIFExpDate) = 1 THEN CONVERT(datetime, CIFExpDate)
      		ELSE CURRENT_TIMESTAMP
      	END
      FROM
          zCIFRecord
      But when I add the WHERE clause it fails:
      Code:
      WHERE CIFExpDate < CURRENT_TIMESTAMP
      This is obviously because the field is only being temporarily updated, so I assigned the values from the Select to a temp variable (calling a convert in the WHERE fails because of my original problems of 00/00/0000)

      Code:
      DECLARE @datestring datetime
      SELECT
          @datestring = 
      	CASE
      		WHEN ISDATE(CIFExpDate) = 1 THEN CONVERT(datetime, CIFExpDate)
      		ELSE CURRENT_TIMESTAMP
      	END
      FROM
          zCIFRecord
      WHERE @datestring < CURRENT_TIMESTAMP
      Then I get the the following error:

      Code:
      "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations."

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Try these clause:


        WHERE convert(case isdate(CIFExpDa te) = 1 then
        CONVERT(datetim e, CIFExpDate) else NULL) < CURRENT_TIMESTA MP
        ORDER BY convert(case isdate(CIFExpDa te) = 1 then
        CONVERT(datetim e, CIFExpDate) else NULL)

        Watch out for the time part. If you need to disregard the timepart, use DATEDIFF instead of simple less than.

        -- CK

        Comment

        • dcharnigo
          New Member
          • Feb 2007
          • 20

          #5
          Thanks, that got me headed in the correct direction and I got it to work, here is the final code for anyone who might want to do something similar:

          Code:
          SELECT
              CIFPan,
              CIFMemNum,
              CIFLName,
              CIFFName,
              CIFInitial,
              CIFExpDate
          FROM
              zCIFRecord
          WHERE
          	CASE 
          		WHEN ISDATE(CIFExpDate) = 1 
          		THEN CONVERT(datetime, CIFExpDate) 
          		ELSE NULL
          	END < CURRENT_TIMESTAMP
          ORDER BY 
          	CASE
          		WHEN ISDATE(CIFExpDate) = 1 
          		THEN CONVERT(datetime, CIFExpDate) 
          		ELSE NULL
          	END ASC
          GO

          Comment

          Working...