Convert from DateTime to INT

后端 未结 3 484
予麋鹿
予麋鹿 2020-12-01 21:57

In my SSIS package, I have to converting values from DateTime to a corresponding INTEGER value. The following sample has been provided.

Any ideas as to how I can con

相关标签:
3条回答
  • 2020-12-01 22:03

    select DATEDIFF(dd, '12/30/1899', mydatefield)

    0 讨论(0)
  • 2020-12-01 22:04

    EDIT: Casting to a float/int no longer works in recent versions of SQL Server. Use the following instead:

    select datediff(day, '1899-12-30T00:00:00', my_date_field)
    from mytable
    

    Note the string date should be in an unambiguous date format so that it isn't affected by your server's regional settings.


    In older versions of SQL Server, you can convert from a DateTime to an Integer by casting to a float, then to an int:

    select cast(cast(my_date_field as float) as int)
    from mytable
    

    (NB: You can't cast straight to an int, as MSSQL rounds the value up if you're past mid day!)

    If there's an offset in your data, you can obviously add or subtract this from the result

    You can convert in the other direction, by casting straight back:

    select cast(my_integer_date as datetime)
    from mytable
    
    0 讨论(0)
  • 2020-12-01 22:23

    Or, once it's already in SSIS, you could create a derived column (as part of some data flow task) with:

    (DT_I8)FLOOR((DT_R8)systemDateTime)
    

    But you'd have to test to doublecheck.

    0 讨论(0)
提交回复
热议问题