convert to date

  • I have a field called DueDates in table where they stored a Date value as Char(10) .

    mm/dd/yy

    How can I get it to true date so I can perform logic like to find all DueDates > 10/01/2022

    I tried cast and convert but when I apply logic DueDates > 10/01/2022 it doesn't find correct output.

    Thanks.

  • error

     

    Msg 206, Level 16, State 2, Line 1

    Operand type clash: date is incompatible with int

  • Odd, CONVERT didn't work for me. So I tried it with CAST

    CREATE TABLE textDates(textDate char(10) not null);
    go

    INSERT INTO textDates(textdate) values ('10/01/2022'),('09/02/2022'),('01/03/2024');

    SELECT *
    FROM (
    SELECT textDate, testDate = CAST(textDate AS DATE)
    FROM textDates
    ) x ORDER BY testDate ASC

    The wrapper nonsense around the inner select was just so I could test the sorting to see if it really converted to a date.

    • This reply was modified 2 days, 16 hours ago by  pietlinden.
  • okay thanks... I think problem is when I try to do a where clause against the CAST(textDate AS DATE) .

     

    where CAST(textDate AS DATE) > mm/dd/yy

     

    I get this

    Msg 206, Level 16, State 2, Line 1

    Operand type clash: date is incompatible with int

  • Maybe your "date" needs quotes

    CONVERT(date, textDate , 1) > CONVERT(date, 'mm/dd/yy', 1)
  • FWIW:

    I forced the CAST on the text date, so in the outer query, the date values are real dates, so I can order and filter them.

    CREATE TABLE textDates(textDate char(10) not null);
    go

    INSERT INTO textDates(textdate) values ('10/01/2022'),('09/02/2022'),('01/03/2024');

    SELECT *
    FROM (
    SELECT textDate, testDate = CAST(textDate AS DATE)
    FROM textDates
    ) x
    WHERE testDate > '2022-09-03'
    ORDER BY testDate ASC
  • Without test data we are just guessing.

    Maybe this will do the trick

    DECLARE @SearchDate date = CONVERT(date, '12/29/2024', 101);

    SELECT *
    FROM DueDates
    WHERE CONVERT(date, textDate , 1) > @SearchDate
    ORDER BY CONVERT(date, textDate , 1);
  • I was able to use CAST(textDate AS DATE) into a #temp table and do the date logic from there.

    Thanks for all responses.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply