Date/hour diff between two date/timestamps - however dataype in DB is curr varchar

Hi All
Could someone please help me?
I would like the T-SQL statement for hours passed between two date/timestamps within a table
e.g
Login_Time
 22/02/2014 11:03:12.925297 AM    (Data type in DB is varchar(50))
Logout_Time
22/02/2014 11:19:01.701073 AM      (Data type in DB is varchar(50))
Thank you Kindly

DECLARE @dt AS VARCHAR(50)='22/02/2014 11:03:12.925297 AM'
DECLARE @dt1 AS VARCHAR(50)='22/02/2014 11:19:01.701073 AM' 
SELECT DATEDIFF(hour,dt_from,dt_to) FROM
SELECT CAST(SUBSTRING(@dt,7,4)+ SUBSTRING(@dt,4,2)+SUBSTRING(@dt,1,2)+
SUBSTRING (@dt,11,17)  AS DATETIME2) dt_from,
CAST(SUBSTRING(@dt1,7,4)+ SUBSTRING(@dt1,4,2)+SUBSTRING(@dt1,1,2)+
SUBSTRING (@dt1,11,17)  AS DATETIME2) dt_to
) AS Der
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence

Similar Messages

  • Diff between two date

    i hv two date type data along with time lets say
    p1 '21-jan-2001 12:43:05'
    p2 '22-jan-2001 13:33:10'
    i want diff between two dates p1-p2 will give only the number of days in between but i want even the time also
    for ex in the above ex the diff etween two days is 1 day 1hr bla bla.......

    Check that link... it will offer you a few alternatives:
    http://www.orafaq.com/faq/how_does_one_get_the_time_difference_between_two_date_columns
    HTH,
    Thierry
    with dates as (
    select to_date('22-jan-2001 13:33:10', 'DD-MON-YYYY HH24:MI:SS') as date1,
    to_date('21-jan-2001 12:43:05', 'DD-MON-YYYY HH24:MI:SS') as date2 from dual)
    SELECT floor(((date1-date2)*24*60*60)/3600)
                || ' HOURS ' ||
                floor((((date1-date2)*24*60*60) -
                floor(((date1-date2)*24*60*60)/3600)*3600)/60)
                || ' MINUTES ' ||
                round((((date1-date2)*24*60*60) -
                floor(((date1-date2)*24*60*60)/3600)*3600 -
                (floor((((date1-date2)*24*60*60) -
                floor(((date1-date2)*24*60*60)/3600)*3600)/60)*60) ))
                || ' SECS ' time_difference
    FROM dates;
    24 HOURS 50 MINUTES 5 SECS Edited by: Thierry H. on Mar 3, 2011 5:51 PM

  • How to calculate the hour difference between two dates?

    hi all,
    how to calculate the hour difference between two dates?
    eg i trying this...
    ((TO_DATE(TO_CHAR(GRNi.reference_date_4,'hh24:mi'),'hh24:mi') -
    TO_DATE(TO_CHAR(NVL(GRNi.reference_date_3,SYSDATE),'hh24:mi'),'hh24:mi'))*24)*60 Act_Hr
    Reg.
    AAK

    Hi
    To break the diff between 2 dates into days, hours, minutes, sec -- you can use the following:
    select to_char( created, 'dd-mon-yyyy hh24:mi:ss' ),
    trunc( sysdate-created ) "Dy",
    trunc( mod( (sysdate-created)*24, 24 ) ) "Hr",
    trunc( mod( (sysdate-created)*24*60, 60 ) ) "Mi",
    trunc( mod( (sysdate-created)*24*60*60, 60 ) ) "Sec",
    to_char( sysdate, 'dd-mon-yyyy hh24:mi:ss' ),
    sysdate-created "Tdy",
    (sysdate-created)*24 "Thr",
    (sysdate-created)*24*60 "Tmi",
    (sysdate-created)*24*60*60 "Tsec"
    from all_users
    where rownum < 50
    HTH
    RangaReddy

  • Need help with finding diff between two dates

    Hi,
    could someone please point me in the right direction.
    I want to substract two dates to get the age of a person. One of the date is sysdate and the other is date of birth. Assuming we remove the time part of the date.
    Best Regards,

    Hi,
    select
    timestamp, sysdate,
    decode(sign(sysdate-timestamp - 1/24), -1,
    round(24*60*(sysdate-timestamp)) || ' minutes old ',
    decode(sign(sysdate-timestamp - 1), -1,
    round(24*(sysdate-timestamp)) || ' hours old ',
    decode(sign(sysdate-timestamp - 14), -1,
    trunc(sysdate-timestamp) || ' days old ',
    decode(sign(sysdate-timestamp - 60), -1,
    trunc((sysdate-timestamp)/7) || ' weeks old ',
    decode(sign(sysdate-timestamp - 365), -1,
    round(months_between(sysdate,timestamp)) || ' months old ',
    round(months_between(sysdate,timestamp)/12,1) || ' years old '
    ))))) age
    from t;
    Regards

  • Diff between Two dates

    I am Unable to find the Diffrence betweeen two dates. can you please help how i find the difference between tow dates.
    Thanks,

    Hi,
    As Bhushan showed, the result of subtracting one DATE from another is the number of days between them.
    If
    d1 is 06:00:00 on April 19, 2010, and
    d2 is 12:00:00 on April 20, 2010, then
    d2 - d1is 1.25, because d2 is 1 day plus 6 hours (6/24 = .25) days after d1.
    If you want the difference in months or years, use the MONTHS_BETWEEN function:
    MONTHS_BETWEEN (d2, d1)is the number of months that d2 is after d1, and
    MONTHS_BETWEEN (d2, d1) / 12is the number of years.

  • How to get the diff between two dates  -  # of days

    Hi Experts,
    I have requirement to know the difference between dates. In my presentation layer two columns as created date and updated date. I want to know the difference in # of days in the presentation services.
    Please provide some pointers.
    Thanks in adv
    svr

    Hi,
    In the presentation layer you can create a column with a custom formula of this:
    TIMESTAMPDIFF(SQL_TSI_DAY, datecolumn1,datecolumn2)
    The result shown in the column will be the difference in days between the two dates.
    Regards,
    Matt

  • Difference between two dates in hours

    hi
    i want to write the procedure which will give me the difference of two dates in hours.
    there are two columns start_dt and end_dt and i want to calculate the difference between them in hours.
    morever i want to add functionality like if the start or end date falls on weekend(saturday & sunday) i want to subtract that whole period(which obviously will be in hours) from the total hours elapsed between two dates.
    could anyone please help me in this?
    regards,
    PD

    nicolas..
    thanks for bringing this into my notice..
    i used ur one of the reply..
    select
    to_char(sysdate,'dd-mm-yyyy hh:mi:ss') d1,
    to_char(sysdate-45.4,'dd-mm-yyyy hh:mi:ss') d2,
    trunc(sysdate-(sysdate-45.4)) "Days",
    trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24) "Hours",
    trunc(((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate
    -45.4)))*24))*60) "Minutes",
    trunc((((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdat
    e-45.4)))*24))*60)-trunc(((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-tru
    nc(sysdate-(sysdate-45.4)))*24))*60)) "Second"
    from dual
    it's working but the problem is that i also have to give functionality like if the start_dt or end_dt is on holiday(i.e. weekend or holiday) i need to subtract the hours spent during holiday period from total hours.
    any help?
    regards,
    PD!!

  • How to get time diff between to date

    Hi all,
    How can I get the time diff. between two date in a single query
    as in the example below-
    select
    to_char(sysdate,'dd-mm-yyyy hh:mi:ss') d1,
    to_char(sysdate-45,'dd-mm-yyyy hh:mi:ss') d2,
    months_between(sysdate,(sysdate-45)) mon_diff
    /* how can I get the time diff in terms of hour ,min, seconds*/
    from dual

    314214, The difference between any two Oracle dates is measured in Days and fractions thereof.
    A day = 24 hrs, = 1440 minutes, = 86400 seconds
    Use the mod function on the difference
    HTH -- Mark D Powell --

  • How to caluclate tat between two dates of one timestamp field

    Hi,
    could some one help how to caluclate days between two dates of single timestamp filed and with this
    query
    Select * from m_activity_transaction where actn_opp_id in (
    Select actn_opp_id from m_activity_transaction where ACTN_ACTV_ID = 218
    Group by actn_opp_id
    having count(*) > 1 ) and ACTN_ACTV_ID = 218
    order by actn_performed_on
    iam getting output is
    ACTN_ID ACTN_OPP_ID ACTN_PERFORMED_ON
    319415 95831 27-JAN-12 11.06.20.000000 AM
    315249 95831 08-FEB-12 05.32.54.000000 PM
    301927 103509 20-DEC-11 04.01.43.000000 PM
    301458 103509 19-DEC-11 04.51.03.000000 PM
    294841 115840 10-JAN-12 03.20.12.000000 PM
    312062 115840 11-JAN-12 05.17.06.000000 PM
    and i nedd to caluclate no.of days between two dates like 27-JAN-12 11.06.20.000000 AM and 08-FEB-12 05.32.54.000000 PM where actn_id is unique AND ACTN_OPP_ID IS NOT UNIQUE.
    Thanks in Advance,
    vvr.

    This way?
    with data as
    select 315249 a, 95831 b, to_timestamp('27-JAN-12 11.06.20.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') dt from dual union all
    select 319415, 95831, to_timestamp('08-FEB-12 05.32.54.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
    select 301927, 103509 , to_timestamp('20-DEC-11 04.51.03.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
    select 301458 , 103509 , to_timestamp('19-DEC-11 04.01.43.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
    select 363810 , 144656 , to_timestamp('27-JUN-12 12.43.28.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
    select 363500 , 144656 , to_timestamp('26-JUN-12 11.41.50.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
    select 363354 , 144656 , to_timestamp('25-JUN-12 12.41.13.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual
    select a, b, diff
      from (
            select a, b, extract( day from (dt - lag(dt) over (partition by b order by dt, a)) ) diff
              from data
           ) tab
    where tab.diff is not null;
    A                      B                      DIFF                  
    319415                 95831                  12                    
    301927                 103509                 1                     
    363500                 144656                 0                     
    363810                 144656                 1

  • Get number of hours between two dates and two hours using factory calendar

    Hello all,
    I have the following requirement: I need to calculate the number of hours between two dates and two hours (start date- finish date and start hour-finish hour) or timestamps using a factory calendar. I must program it on CRM environment.
    Does anybody know a function module that makes it?
    Thanks in advance.
    Carmen

    Please check function module DURATION_DETERMINE.
    - April King

  • Select entries between two dates by converting Unix timestamp in Oracle Dat

    Hi,
    I need to select the entries between two dates from an Oracle db. The Oracle db has a column with Unix timestamps. I use the following querry, but it doesnt seem to be working as desired.
    select count(*) from reporter_status where to_char(FIRSTOCCURRENCE, 'mm-dd-yy') between ('08-07-06') and ('08-08-06');
    FIRSTOCCURRENCE has the Unix timestamps.
    Could anyone help me out with this. Thank you for your help.

    Assuming that it is actually a UNIX timestamp, then it is the number of seconds since Jan 1, 1970, so you need something like:
    SELECT COUNT(*)
    FROM reporter_status
    WHERE TO_DATE('01-Jan-1970', 'dd-mon-yyyy') + (firstoccurrence/24/60/60)
                    BETWEEN TO_DATE('08-07-2006', 'mm-dd-yyyy') AND
                            TO_DATE('08-08-2006, 'mm-dd-yyyy');Did Y2K not teach us anything? Use 4 digit years.
    John

  • Difference between two dates: a complete solution

    There have been many posts asking how to get the number of days between two dates. There is no method in java like the VB method dateDiff(), and there is only the method date.getTime() which gives you the number of milliseconds since Jan 1 1970, the Epoch. I have not seen a solution that makes use of the fact that there are 86400000 milliseconds per day, only when trying to get the number of days , i.e. multiples of 24 hours between two millisecond points in time.
    So what if date1 was Oct 26th 11.30pm and date2 was Oct 27th 12.30am? If you get the millisecond difference, and divide by 86400000, you do not notice that these two times are on separate days, but only one hour apart.
    My solution takes account of the modulo (%) function. If you use the date.getTime() method, and use % 86400000 on this number of milliseconds, it will tell you how far (milliseconds) this date is into this day, after 12am. If you subtract this amount from the total number of milliseconds, then divide by 86400000, this will give you the number of days after the Epoch, so you are able to know which day the millisecond time is in. If you want to know the days between two millisecond times, this will tell you, whereas just dividing by 86400000, will not, and may be misleading.
    I think this could be incorporated into the Date class easily. There are some problems though, as not every day has 86400000 milliseconds, if you are talking about days when Daylight Saying Time begins or ends. Doing calculations like that will create errors, unless you know whether each date is in Daylight Saving Time or not. Leap years are OK, unless want to work out the number of years from the number of days by dividing by 365, as calculating numbers of days is only done by looking at a 24 hour time period. However where 'leap seconds' have been added, like once every ten years, there may be small errors. This was not included in anyone elses calculations.
    So the calculation would go like this:
    public class BetterDate extends Date
    public int dayDifference(Date d)
    long time = this.getTime() ; // gets milliseconds
    long timed = d.getTime() ; // gets milliseconds of other date
    int days = (time - (time % 86400000))/86400000 ; // number of days
    int daysd = (timed - (timed % 86400000))/86400000 ; // number of days of other date
    int difference = daysd - days ; // difference in days between two dates
    return difference ;
    If you want you can use the value (time % 86400000), and modulo this amount with the number of milliseconds in one hour, then subtract the result from the (time % 86400000) and divide by the millliseconds in one hour to give you the number of hours, but there are JDK functions that you can use to get the hour if needed.
    This method is not perfect, but for most cases will get you the difference in days between two dates.
    James

    Hi James,
    I tried out your solution, but it seems to fail on dates without times. I tries the difference between 31/03/2002 and 01/04/2002 and the result is 0. I guess it is because the times are assumed to be 00:00 as no times are mentioned and 31st March I believe is the date when DST change will take place this year. If I set the two times as well to a time much later than midnight for example 0600 then I seem to get the right answer.
    Joag
    There have been many posts asking how to get the
    number of days between two dates. There is no method
    in java like the VB method dateDiff(), and there is
    only the method date.getTime() which gives you the
    number of milliseconds since Jan 1 1970, the Epoch. I
    have not seen a solution that makes use of the fact
    that there are 86400000 milliseconds per day, only
    when trying to get the number of days , i.e. multiples
    of 24 hours between two millisecond points in time.
    So what if date1 was Oct 26th 11.30pm and date2 was
    Oct 27th 12.30am? If you get the millisecond
    difference, and divide by 86400000, you do not notice
    that these two times are on separate days, but only
    one hour apart.
    My solution takes account of the modulo (%) function.
    If you use the date.getTime() method, and use %
    86400000 on this number of milliseconds, it will tell
    you how far (milliseconds) this date is into this day,
    after 12am. If you subtract this amount from the total
    number of milliseconds, then divide by 86400000, this
    will give you the number of days after the Epoch, so
    you are able to know which day the millisecond time is
    in. If you want to know the days between two
    millisecond times, this will tell you, whereas just
    dividing by 86400000, will not, and may be
    misleading.
    I think this could be incorporated into the Date class
    easily. There are some problems though, as not every
    day has 86400000 milliseconds, if you are talking
    about days when Daylight Saying Time begins or ends.
    Doing calculations like that will create errors,
    unless you know whether each date is in Daylight
    Saving Time or not. Leap years are OK, unless want to
    work out the number of years from the number of days
    by dividing by 365, as calculating numbers of days is
    only done by looking at a 24 hour time period. However
    where 'leap seconds' have been added, like once every
    ten years, there may be small errors. This was not
    included in anyone elses calculations.
    So the calculation would go like this:
    public class BetterDate extends Date
    public int dayDifference(Date d)
    long time = this.getTime() ; // gets milliseconds
    long timed = d.getTime() ; // gets milliseconds of
    other date
    int days = (time - (time % 86400000))/86400000 ; //
    number of days
    int daysd = (timed - (timed % 86400000))/86400000 ; //
    number of days of other date
    int difference = daysd - days ; // difference in days
    between two dates
    return difference ;
    If you want you can use the value (time % 86400000),
    and modulo this amount with the number of milliseconds
    in one hour, then subtract the result from the (time %
    86400000) and divide by the millliseconds in one hour
    to give you the number of hours, but there are JDK
    functions that you can use to get the hour if needed.
    This method is not perfect, but for most cases will
    get you the difference in days between two dates.
    James

  • Calculate difference between two dates/times

    Hi all,
    Is there any function module to calculate difference between two dates/times which are in TIMESTAMPL format.
    I need to know how many millisconde(second,minutes, hours... )there is between these two times.
    Please, It is urgent
    Thank you all.
    Karim

    hi,
    try the following function
    CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'        
          EXPORTING                                  
               timestamp1 = timestamp1               
               timestamp2 = timestamp2               
          IMPORTING                                  
               difference = diff                     
          EXCEPTIONS                                 
               OTHERS     = 1. 
    the above function gives the difference in seconds...
    try the following  code to set the resolution to milliseconds..
    SET RUN TIME CLOCK RESOLUTION LOW
    check the thread for details:
    SET RUN TIME CLOCK RESOLUTION?
    all the best!!!
    Regards,
    Aparna

  • Calculate the difference between two dates times in infopath form 2013

    Hi,
    I have an infopath 2013 form that contains three fields:
    2 date time and the 3rd contains the difference between the two in hours
    how I can make the difference between the two so that the display will be like this:
    Date Time1           08/21/2014           22:00 
    Date Time2           08/22/2014           1:00
    Diff Field                                             3:00

    Hi,
    Please refer to the following article which matches your requirement exactly.
    Calculate the difference between two date picker controls in InfoPath using rules and formulas - no code!
    Please mark it answered, if your problem resolved.

  • As to the data type of the data type of the difference between two date type of datas

    Hi,
    I have a question about the data type of the difference between two date type of datas.
    There are two date type of datas as:
    SSHIPMENTS.RECEIVEDATETIME
    SSHIPMENTS.PROMISEDATETIME
    I try to use the following SQL Script in Oracle SQL*Plus as:
    SELECT CASE
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:00:00.000' THEN 'OnTime'
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    The error message of "Invalid Number" for the '000 01:30:00.000' happens.
    I don't know if the data type of the interval is wrong.
    Many Thanks,
    Cathy

    SELECT CASE
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss')) < '010000' THEN 'OnTime'
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss'))< '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    just try it out..

Maybe you are looking for

  • Edit Problem after changing and transporting a table view

    Hallo Experts, I hope you can help me. I changed a Z-table in the Data Dictionary. For this table there exists an Tableview. After I added three new fields I generate a new tableview: Utilities -> table maintenance generator Change -> create maintena

  • OUTLOOK PROFILE - UNABLE TO SIGN IN

    IN LYNC 2013 THE LOWER PORTION OF THE DIALOG  BOX HAS A LINK THAT READS "VIEW MORE IN OUTLOOK" THE ERROR MESSAGE REPORTS THE FOLLOWING: THE EMAIL ADDRESS YOU USED IN YOUR DEFAULT OUTLOOK PROFILE IS DIFFERENT FROM THE SIGN-IN ADDRESS USED IN LYNC. CHA

  • Probook 6560b not turning on (blinking power light)

    Can someone help  me with this issue?

  • Names of the reports

    hi friends can anybody send me some names of the reports in SD and MM modules...like purchase order to payments, asset aging.... and i would like to know the fields in each report.... thanks in advance deepa

  • Which Ports to use?

    I am kinda new to skype and don't know a lot about it. So i was just going through the options and stuffs ...and under the ''Advanced'' tab there's ''connection''..and it has a lot of options.. like ''use port ..for incoming connections'' ''enable up