Converting varchar to Int for date difference

  • Hi Experts,

    Constraints: I do not have create permissions and I am learning

    This is my code

    Basically testing to fix the T-SQL putting junk 1900 dates and if valid then find the difference between FADate and TDate

    Unfortunately, even after trying to convert the varchar to Int using convert(int, a.FAdate) I get conversion error.

    The other way is to first find the difference using datediff and verify which date is 1900 to put a string as I need to collect which record has valid, invalid dates but i need to do check for zero difference or Huge difference.

    Any smart way to resolve this please ?

    ,IIF(
    (CONVERT(varchar(10),a.FADate,105)) > '01-01-1990' ,
    IIF(
    (CONVERT(varchar(10),a.TDate,105)) > '01-01-1990' , DATEDIFF(d,CONVERT(INT,a.FADate), CONVERT(INT,a.TDate)),
    'NoTDate'),
    'NoFADate'
    ) AS DateDiffIfVerified

    • This topic was modified 2 years, 7 months ago by  protocoder.
  • Don't convert a columns data type if it is not needed

    If conversion is needed, convert to the correct data type.

    Declare @tb table ( FADate date not null, TDate date not null )

    Insert into @tb values ( '2021-01-01', '2022-01-01' ),( '1899-01-01', '2022-01-01' )


    Declare @DtRef date = '1900-01-01'
    SELECT IIF( a.FADate > @DtRef,
    -- true
    IIF(a.TDate > @DtRef,
    --true
    -- converting to varchar because of the use of text 'NoTdate' and 'NoFADate'
    convert(varchar(25), DATEDIFF(d, a.FADate, a.TDate) )
    , 'NoTDate'),

    -- false
    'NoFADate') AS DateDiffIfVerified

    from @tb a;

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • --deleted

    • This reply was modified 2 years, 7 months ago by  Phil Parkin.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Btw, I think it would be better if you used a separate column for the string message to keep the diff column as only numeric, but that's just IMO.

    SELECT 
    CASE WHEN a.FADate <= '19900101' THEN 'NoFADate'
    WHEN a.TDate <= '19900101' THEN 'NoTDate'
    ELSE CAST(DATEDIFF(DAY, a.FADate, a.TDate) AS varchar(8)) END AS DateDiffIfVerified

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Thank you @JohanBijnens, Great help, I will test this smart stuff.

    @scottpletcher, wow for simple and great idea. I will remember this easy technique. To both of you thanks a ton.

    • This reply was modified 2 years, 7 months ago by  protocoder.

Viewing 5 posts - 1 through 4 (of 4 total)

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