Extract Time from date and Time and Need XLMOD Funtion to find the Difference between Two Time.

X6 = "1/5/15 5:16 AM" & NOW ....................difference by Only Time
not date
X6 date and Time will be changing, Its not Constant
            Dim myDateTime As DateTime = X6
            Dim myDate As String = myDateTime.ToString("dd/MM/yy")
            Dim myTime As String = myDateTime.ToString("hh:mm tt")
            Dim myDateTime1 As DateTime = Now
            Dim myDate1 As String = myDateTime1.ToString("dd/MM/yy")
            Dim myTime1 As String = myDateTime1.ToString("hh:mm tt")
Need to use this function to find the Difference between Two Time. due to 12:00 AM isuue
Function XLMod(a, b)
    ' This replicates the Excel MOD function
    XLMod = a - b * Int(a / b)
End Function
Output Required
 dim dd  = XLMod(myTime - myTime1)
Problem is myTime & myTime1 is String Need to convert them into Time, Later use XLMOD Funtion.

Induhar,
As an addendum to this, I thought I'd add this in also: If you have two valid DateTime objects you might consider using a class which I put together a few years ago
shown on a page of my website here.
To use it, just instantiate with two DateTime objects (order doesn't matter, it'll figure it out) and you'll then have access to the public properties. For this example, I'm just showing the .ToString method:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim date1 As DateTime = Now
Dim date2 As DateTime = #1/1/1970 2:35:00 PM#
Dim howOld As New Age(date1, date2)
MessageBox.Show(howOld.ToString, "Age")
Stop
End Sub
End Class
I hope that helps, if not now then maybe at some point in the future. :)
Still lost in code, just at a little higher level.
  Thanx frank, can use this in Future....

Similar Messages

  • Function Module to find the Difference between two times.

    Hi All,
    Wud you plz let me know the Function Module to find the Difference between two times.
    Input Time1( Hours:Minutes) Time2 ( Hours:Minutes)
    Need Output in Hours:Minutes only . ( No seconds Needed )
    Ex :
    Input :
           06:00 to 18:00 Output : 12:00
    and  20:00 to 06:00 Output: 10:00 with +ve sign only. No -ve sign.
    Thanks,
    N.L.Narayana

    check this .
    data : p_timel like sy-uzeit,
           p_timeh like sy-uzeit,
           diff like sy-uzeit,
           di(8) type c .
           p_timel = '200000'.
           p_timeh = '060000'.
           diff = p_timeh - p_timel.
           concatenate diff+0(2) ':' diff+2(2) into di.
           write:/ di.
    also check for this.
           p_timel = '060000'.
           p_timeh = '180000'.
    see if this can be implemented in ur code .
    or else  u can try with  Fm L_TO_TIME_DIFF passing startdate enddate starttime endtime with UOM as MIN
    hope this helps regards,
    vijay

  • Help I need to subtract times to find the difference between two times?

    Hello there, I really need help.
    I'm trying to make this attendance tracking system so that when a user enters his or her ID the system wll remember his/her check in time. However I also need to find out either how late the person is or how early to store their over time and late.
    So I get the time by using a timestamp, so I'm left with a timestamp that I store into the db. But the problem comes when trying to compute for the late or over time. To do that I have to get the normal login/checkin time from the employee schedules table in the db and either subtract that time from the checkInTime to compute for how late or subtract the checkIn time from it to find early.
    The thing is, the schedule is stored in datetime format in the db since the db is sql. Also I have no idea on how to subtract the two dates via java. The month, date and year stored in the sql db are just fillers, all I need computed is the time difference of the hours and minutes between the checkInTime and the schedule.
    I've tried to use datediff: SELECT e.employeeNumber, datediff('hh', startTime, checkInTime) as lateTime from employeeschedules e, timandattendance, contractualemployees c where c.scheduleNumber=e.scheduleNumber and t.employeeNumber=c.employeeNumber and t.checkInNumber=1;
    But it keeps on giving out various errors: You have an error in your sql syntax check your manual that corresponds to your sql server version. for the right syntax to use near , checkInTime) as lateTime from employeeschedules e, timandattendance, contractualemployees c where c.scheduleNumber=e.scheduleNumber and t.employeeNumber=c.employeeNumber and t.checkInNumber=1;
    My sql server is My SQL 5.0.5
    Now I'm thinking of useing Gregorian instead, but I don't know how to use Gregorian and dont know the various methods. Can someone tell me what to do, provide some sample codes and references that I can use to accomplish what I need to do?

    NewtonsApple_2 wrote:
    Now I'm thinking of useing Gregorian instead, but I don't know how to use Gregorian and dont know the various methods. Can someone tell me what to do, provide some sample codes and references that I can use to accomplish what I need to do?it may help...
    GregorianCalendar

  • How to do find the difference between two time stamps

    Hi all,
    i have a table with 2 columns called GMT time and Local time . so i need to find difference between these two time stamps.
    i tried like this
    select to_date(GMT_TIME,'yyyy-mm-dd hh24:hi:ss')-to_date(LOCAL_TIME,'yyyy-mm-dd hh24:hi:ss') from date_table
    the result is coming as follows ..
    0.291666666667
    i did not under stand the out put.
    In my table GMT_TIME= 2011-06-26 00:00:00 and LOCAL_TIME=2011-06-25 17:00:00 ..
    please help me how to get exact hours between two dates ..
    Thanks
    Sreedhar

    Hi Sreedhar,
    Your output (0.2916666) has the unit day.
    You should multiply with 24 to get the unit hour.
    select ( to_date(GMT_TIME,'yyyy-mm-dd hh24:hi:ss')-to_date(LOCAL_TIME,'yyyy-mm-dd hh24:hi:ss') ) * 24 from date_table;Rgds,
    Tycho

  • How to find the difference between 2 time values in Java

    hi all,
    i have 2 time values
    String time1="6:20";
    String time2="21:30"
    How to find the difference between 2 times in Java?
    pls help
    thanx in advance....

    Calculating Java dates: Take the time to learn how to create and use dates
    Working in Java time: Learn the basics of calculating elapsed time in Java
    Formatting a Date Using a Custom Format
    Parsing a Date Using a Custom Format

  • Need to find the Difference between two table

    Hello ,
    I have stucked in program as below scenario:-
    I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
    By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
    Example TableA
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    80 DEF 6005 YY 446 YY8
    TableB
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    81 DEF 6005 Yu 447 YY8
    I need to the out put like this :-
    The Diffence between two table is
    TableA.COL1=80 TableB.Col1=81, Different
    TableA.Col4=YY TableB.col4=Yu,Different
    TableA.Col5=446TableB.col5=447,Different
    Please suggest me to write the pl/sql program for the same
    thanx in advance
    KK

    Thanx friends for all your efforts
    I have a sample code for the same,this will compare the two tables for single row in each table .
    what r the modification needed for the multiple rows of values in the two tables??
    Please suggest!!
    CREATE OR REPLACE PROCEDURE test_compare
    IS
    TYPE t_col
    IS
    TABLE OF VARCHAR2 (30)
    INDEX BY PLS_INTEGER;
    l_col t_col;
    j NUMBER := 0;
    l_sql VARCHAR2 (2000);
    col1 VARCHAR2 (30);
    col2 VARCHAR2 (30);
    val1 NUMBER;
    val2 NUMBER;
    status VARCHAR2 (30);
    CURSOR c1
    IS
    SELECT column_id, column_name
    FROM all_tab_columns
    WHERE table_name = 'TEST1';
    BEGIN
    FOR i IN c1
    LOOP
    j := j + 1;
    l_col (j) := i.column_name;
    END LOOP;
    FOR k IN 1 .. j
    LOOP
    l_sql :=
    'SELECT '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST2.'
    || l_col (k)
    || ', '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST1.'
    || l_col (k )
    || ', '
    || 'DECODE(TEST2.'
    || l_col (k)
    || ' -TEST1.'
    || l_col (k)
    || ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
    EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
    IF status = 'CHANGED'
    THEN
    DBMS_OUTPUT.put_line( 'TEST2.'
    || col1
    || '='
    || val1
    || ', TEST1.'
    || col2
    || '='
    || val2
    || ', '
    || status);
    END IF;
    END LOOP;
    EXCEPTION
    WHEN OTHERS
    THEN
    DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
    END;
    /

  • Function Module to get the difference between two times.

    Hi All,
    I want to know if there is any function module that gives me the difference between two specified times.
    For Ex: Time 1: 12/01/2007 00:01 A.M
                Time 2: Time 1 - 180 Seconds. This changes the time, also may even change the Date. (As in above situation, the Time 2 will be 11/30/2007 11:58 P.M)
    Could some one please suggest if there is any function module for this case.
    Thanks in Advance.

    Hi, You can use the following function module to calculate the diffrence between to dates in the unit that you want.
    COPF_DETERMINE_DURATION calculates the difference between two dates and time in minutes and hours.
    Parameters:
    EXPORTING
         I_START_DATE "The start date of the time interval
         I_START_TIME "The start time of the time interval
         I_END_DATE   "The end date of the time interval
         I_END_TIME   "The end time of the time interval
         I_UNIT_OF_DURATION "Time unit of the duration if not to be calculated in days.
         Value     =====     Meaning
         ' '     =====     Day (default)
         D     =====     Days
         H     =====     Hours
         MIN     =====     Minutes
         MON     =====     Months
         S     =====     Seconds
         WK     =====     Weeks
         YR     =====     Years
         I_FACTORY_CALENDER     "A factory calender if not using the Gregorian calender
    IMPORTING
         E_DURATION      "Time difference in unit specified.
    Message was edited by:
            Rajesh Soman

  • FormCalc Formula to tell the difference between two time fields if they are populated - Help please

    Hi,
    I have a cell (TotalTime1) that references two other time formatted cells (Start1 and Finish 1).
    The below formula is intended to tell the difference between the Start and Finish time if they are populated.
    I had it working without the HasValue condition but the formula was calculating when there was no value in Start1 and Finish1 (due to time format).
    This is the code I do have, it says there is an error near the last line; I am sorry if I post this in the wrong manner, I have not posted on this forum before but here goes:
    if (HasValue (Finish1)  and HasValue (Start1)) then if (Time2Num(Start1.formattedValue, "HH:MM") < Time2Num (Finish1.formattedValue, "HH:MM")) then Abs (Time2Num(Start1.formattedValue, "HH:MM") - Time2Num(Finish1.formattedValue, "HH:MM")) / (60 * 60 * 1000) else 24 - Abs (Time2Num(Finish1.formattedValue, "HH:MM") - Time2Num(Start1.formattedValue, "HH:MM")) / (60 * 60 * 1000) endif 
    Any help would be appreciated.
    Regards Fetachini

    Thanks whyisthisme,
    Your speedy response is greatly appreciated, your support has resolved my issue.
    Many thanks.
    Regards
    Fetachini

  • Find the difference between two dates for the specific month and year

    Hi,
    I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
    for that date. In this case I input Jan 2013. How can I sql this ?

    I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
    First let me understand you:
    You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
    declare @for_month int = 1 --January
    declare @for_year int = 2013
    declare @StartDate date = '2012-12-20'
    declare @EndDate date = '2013-01-04'
    SELECT
    CASE
    WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
    DATEDIFF(DAY, @StartDate,@EndDate)
    WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
    WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
    WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
    DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
    ELSE
    0
    END as [DD]
    I don't know how you calculate day range between 01/01/2013 and 04/01/2013
    is 4, it is actually is 3 but if that is the case, you can add 1 from the condition.

  • How do I calculate the difference between two times?

    I am so embarrassed by the fact that I can't figure this out.
    Cell B2- 8:00 am
    Cell C2- 10:50 am
    Cell D2- (How do I get this cell to calculate the difference and say 2:50?)
    I know this is probably one of the most basic operations, but for the life of me I can't figure it out. Cells B2 & C2 are formatted for 24 hour clock. But if I tell the system to just subtract the two, I get "0.118". Everything I find on the forum search goes beyond what I need. Can anyone help me?
    Thank you.

    KOENIG Yvan wrote:
    Numbers states clearly in the Help and the PDF Users Guide that it doesn't know a "duration" object but a time one which is restricted to the range 00:00:0 to 23:59:59.
    When I search the U.S. language Numbers User Guide for the word "duration," it is not found.
    What may be more clear: _duration is not available but time is_?
    Once again your response resemble to a rant againt the Help and the User Guide.
    In the Help:
    +date-time Any Numbers date/time value. _While you can choose to display only date or time in a cell, all Numbers date or time values contain both the date and time._+
    Which wording would be more clear and precise?
    TIMEVALUE
    +The TIMEVALUE function converts a date, a time, or a text string to _a decimal fraction of a 24-hour day._+
    Which wording would be more clear and precise?
    TIME
    +The TIME function converts hours, minutes, and seconds into a time format.+
    +TIME(hours, minutes, seconds)+
    +hours: The number of hours _(using a 24-hour clock)._+
    +minutes: The number of minutes.+
    +seconds: The number of seconds.+
    Notes
    +You can specify hour, minute, and second values greater than 23, 59, and 59, respectively. _If the hours, minutes, and seconds add up to more than 24 hours, Numbers subtracts 24 hours repeatedly until the sum is less than 24 hours._+
    Which wording would be more clear and precise?
    In the User Guide:
    page 190
    +date-time Any Numbers date/time value. _While you can choose to display_+
    +_only date or time in a cell, all Numbers date or time values contain_+
    +_both the date and time._+
    +TIME (page 277) Converts a time to a decimal fraction of a 24-hour day.+
    +TIMEVALUE (page 278) Converts a time in a string to a decimal fraction of a 24-hour day.+
    TIME
    +The TIME function converts the specified time to a decimal fraction of a 24-hour day.+
    +TIME(hours, minutes, seconds)+
    +• hours: The number of hours _(using a 24-hour clock)_.+
    +• minutes: The number of minutes.+
    +• seconds: The number of seconds.+
    Notes
    +You can specify hour, minute, and second values greater than 23, 59, and 59,+
    +respectively. _If the hours, minutes, and seconds add up to more than 24 hours,_+
    +_Numbers subtracts 24 hours repeatedly until the sum is less than 24 hours._+
    +You can also specify fractional values for hours, minutes, or seconds.+
    TIMEVALUE
    +The TIMEVALUE function converts a time in a string to a decimal fraction of a 24-hour+
    day.
    TIMEVALUE(date-time)
    +• date-time: A date, a time, or a string in any of the Numbers date and time formats.+
    As you may check, the infos are exactly the same in the Help and in the Guide.
    And I really don't understand how you may find them unclear.
    Yvan KOENIG (from FRANCE lundi 4 août 2008 14:57:36)

  • How to find the difference between two date?

    Hi,
    I currently writing a date comparision program. Below is the idea analogy,
    Currently i need to find how many day differences between 30 July 2003 and 22 June 2004. How can i use java to code it?
    Thanks.

    there doesn't seem to be a direct way but try this:int daysBetween = 0;
    Calendar c = new GregorianCalendar(2004, Calendar.JULY, 30);
    Calendar d = new GregorianCalendar(2003, Calendar.JUNE, 22);
    while (c.get(Calendar.YEAR) != d.get(Calendar.YEAR)) {
        daysBetween += 360;
        d.add(Calendar.DAY_OF_YEAR, 360);
    daysBetween += c.get(Calendar.DAY_OF_YEAR) - d.get(Calendar.DAY_OF_YEAR);This gives the correct result of 404 (= 8 days from June 22 to June 30 + 366 days between July 1 of 2003 and 2004 + 30 days from July 1 to July 30)

  • Function Module to find the Diff bt Two Times : O/p : Hours:Minutes only

    Hi All,
    Wud you plz let me know the Function Module to find the Difference between two times.
    Input Time1( Hours:Minutes:Seconds) Time2 ( Hours:Minutes:Seconds)
    Need Output in Hours:Minutes only .  ( No seconds Needed )
    Thanks,
    N.L.Narayana

    Hi
    Please use FM
    <b>SD_DATETIME_DIFFERENCE</b>

  • Find the difference between loading and registering the drivers..

    Dear Sir..
    Could you please help me to find the difference between two activities..
    1.Loading the drivers
    2.Registering the drivers
    What's the difference between loading and registeing the drivers and what activities take place by the JVM to do it all.

    Dear Sir..
    Could you please help me to find the difference
    between two activities..
    1.Loading the drivers
    2.Registering the drivers
    What's the difference between loading and registeing
    the drivers and what activities take place by the JVM
    to do it all.You load a class - it isn't specific to a driver.
    That is part of java - not JDBC.
    Normally JDBC drivers register themselves when the class is loaded. This is specific to the driver and has nothing to do with a user of the driver. It is only a concern to someone who must implement a driver.

  • How to read time stamps from a spreadsheet and calculate the difference between consecutive time stamps?

    Hi,
    I am new to Labview. This question might be a joke to some of you here, but any help would be greatly appreciated. I have a spreadsheet with time stamps and power outputs from a generator. I am supposed to calculate the difference between consecutive time stamps, which will act as a delay for the next power output update that needs to be sent. For example, lets say that I have to following data:
    Time Stamp                    Power Output
    11:00:00 AM                       3kW
    11:00:02 AM                       2.9kW
    11:00:04 AM                       3.2kW
    11:00:06 AM                       3.1kW
    The above data doesn't make any sense, but it is just for the purpose of this question.
    So, I have to read 11:00:00 AM and 3kW initially - 3kW is the initial request that is sent. Then I have to

    Repeated forum post
    Please view http://forums.ni.com/ni/board/message?board.id=170&message.id=294435
    Regards,
    Juan Galindo
    Applications Engineer
    National Instruments

  • Query to find the difference between the last date and the second to the last date

    Hi all,
    Hope all is well.
    I am working on the following problem because I am trying to improve my MS SQL skills. But I am stuck at the moment and I wonder if you could provide some assistance please. Here is the issue:
    Table 1: Dividends
    divId
    ExDate
    RecordDate
    PayDate
    Amount
    Yield
    symId
    1
    2013-02-19
    2013-02-21
    2013-03-14
    0.23
    0.00000
    3930
    2
    2012-11-13
    2012-11-15
    2012-12-13
    0.23
    0.00849
    3930
    3
    2012-08-14
    2012-08-16
    2012-09-13
    0.20
    0.00664
    3930
    4
    2012-05-15
    2012-05-17
    2012-06-14
    0.20
    0.00662
    3930
    5
    2012-02-14
    2012-02-16
    2012-03-08
    0.20
    0.00661
    3930
    6
    2011-11-15
    2011-11-17
    2011-12-08
    0.20
    0.00748
    3930
    7
    2011-08-16
    2011-08-18
    2011-09-08
    0.16
    0.00631
    3930
    8
    2011-05-17
    2011-05-19
    2011-06-09
    0.16
    0.00653
    3930
    9
    2011-02-15
    2011-02-17
    2011-03-10
    0.16
    0.00594
    3930
    10
    2010-11-16
    2010-11-18
    2010-12-09
    0.16
    0.00620
    3930
    11
    2010-08-17
    2010-08-19
    2010-09-09
    0.13
    0.00526
    3930
    12
    2010-05-18
    2010-05-20
    2010-06-10
    0.13
    0.00455
    3930
    13
    2010-02-16
    2010-02-18
    2010-03-11
    0.13
    0.00459
    3930
    Table 2: Tickers
    symId
    Symbol
    Name
    Sector
    Industry
    1
    A
    Agilent Technologies Inc.
    Technology
    Scientific & Technical Instruments
    2
    AA
    Alcoa, Inc.
    Basic Materials
    Aluminum
    3
    AACC
    Asset Acceptance Capital Corp.
    Financial
    Credit Services
    4
    AADR
    WCM/BNY Mellon Focused Growth ADR ETF
    Financial
    Exchange Traded Fund
    5
    AAIT
    iShares MSCI AC Asia Information Tech
    Financial
    Exchange Traded Fund
    6
    AAME
    Atlantic American Corp.
    Financial
    Life Insurance
    7
    AAN
    Aaron's, Inc.
    Services
    Rental & Leasing Services
    8
    AAON
    AAON Inc.
    Industrial Goods
    General Building Materials
    9
    AAP
    Advance Auto Parts Inc.
    Services
    Auto Parts Stores
    10
    AAPL
    Apple Inc.
    Technology
    Personal Computers
    11
    AAT
    American Assets Trust, Inc.
    Financial
    REIT - Office
    12
    AAU
    Almaden Minerals Ltd.
    Basic Materials
    Industrial Metals & Minerals
    I am trying to check the last date (i.e. max date) and also check the penultimate date (i.e. the second to the last date).  And then find the difference between the two (i.e. last date minus penultimate
    date).
    I would like to do that for each of the companies listed in Table 2: Tickers.  I am able to do it for just one company (MSFT) using the queries below:
    SELECT
    [First] = MIN(ExDate),
    [Last] = MAX(ExDate),
    [Diff] = DATEDIFF(DAY, MIN(ExDate), MAX(ExDate))
    FROM (
    SELECT TOP 2 Dividends.ExDate
    FROM Dividends, Tickers
    WHERE Dividends.symId=Tickers.symId
    AND Tickers.Symbol='MSFT'
    ORDER BY ExDate DESC
    ) AS X
    Outputs the following result:
    First
    Last
    Diff
    2012-11-13
    2013-02-19
    98
    But what I would like instead is to be able to output something like this:
    Symbol
    First
    Last
    Diff
    MSFT
    2012-11-13
    2013-02-19
    98
    AAN
    2012-11-13
    2012-12-14
    1
    X
    2012-11-13
    2012-12-14
    1
    Can anyone please let me know what do I need to add on my query in order to achieve the desired output?
    Any help would be greatly appreciated.
    Thanks in advance. 

    Could you try this?
    create table Ticker (SymbolId int identity primary key, Symbol varchar(4))
    insert into Ticker (Symbol) values ('MSFT'), ('ORCL'), ('GOOG')
    create table Dividend (DividendId int identity, SymbolId int constraint FK_Dividend foreign key references Ticker(SymbolId), ExDate datetime, Amount decimal(18,4))
    insert into Dividend (SymbolId, ExDate, Amount) values
    (1, '2012-10-1', 10),
    (1, '2012-10-3', 1),
    (1, '2012-10-7', 7),
    (1, '2012-10-12', 2),
    (1, '2012-10-23', 8),
    (1, '2012-10-30', 5),
    (2, '2012-10-1', 10),
    (2, '2012-10-6', 1),
    (2, '2012-10-29', 7),
    (3, '2012-10-1', 22),
    (3, '2012-10-3', 21),
    (3, '2012-10-7', 3),
    (3, '2012-10-12', 9)
    WITH cte
    AS (SELECT t.Symbol,
    d.ExDate,
    d.Amount,
    ROW_NUMBER()
    OVER (
    partition BY Symbol
    ORDER BY ExDate DESC) AS rownum
    FROM Ticker AS t
    INNER JOIN Dividend AS d
    ON t.SymbolId = d.SymbolId),
    ctedate
    AS (SELECT Symbol,
    [1] AS maxdate,
    [2] AS penultimatedate
    FROM cte
    PIVOT( MIN(ExDate)
    FOR RowNum IN ([1],
    [2]) ) AS pvtquery),
    cteamount
    AS (SELECT Symbol,
    [1] AS maxdateamount,
    [2] AS penultimatedateamount
    FROM cte
    PIVOT( MIN(Amount)
    FOR RowNum IN ([1],
    [2]) ) AS pvtquery)
    SELECT d.Symbol,
    MIN(MaxDate) AS maxdate,
    MIN(penultimatedate) AS penultimatedate,
    DATEDIFF(d, MIN(penultimatedate), MIN(MaxDate)) AS numberofdays,
    MIN(MaxDateAmount) AS maxdateamount,
    MIN(penultimatedateAmount) AS penultimatedateamount,
    MIN(MaxDateAmount) - MIN(penultimatedateAmount) AS delta
    FROM ctedate AS d
    INNER JOIN cteamount AS a
    ON d.Symbol = a.symbol
    GROUP BY d.Symbol
    ORDER BY d.Symbol
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers.
    Thanks!
    Aalam | Blog (http://aalamrangi.wordpress.com)

Maybe you are looking for

  • Not working across managed code - PDPageDrawContentsToWindow - HDC

    PageDLL - a Com Dll contains the following: struct GRect_tag     long lLeft;     long lBottom;     long lTop;     long lRight;     }  GRect; BOOL CMyDrawMC::SetDrawPageToWindow(int hdc, GRect WinRect) PDDoc objPDDoc; PDPage objPDPage; objPDDoc = PDDo

  • IChat USB problem with seemingly no solution

    I am working on a PowerPC g5(10.4.10) and am trying to get my webcam(Logitech Quickcam 4000) to work through a program called iChat USB(http://www.ecamm.com/mac/ichatusbcam/). The webcam worked last year on my mini mac, but after moving to a new comp

  • Problems calling image on J2ME mobile phone screen

    I have an image size (160kb) of certain size and width. I am calling this image onto a mobile phone screen (j2me) and upon calling this image, I am getting OutOfMemoryException. Sometimes, I do get the partial image displayed on the phone screen. My

  • Is there a Webclient UI in SRM?

    Hi Gurus! I'm starting looking at SRM and I couldn't find many information about it. Can you please tell me if this works with a Webclient UI (like CRM 7.0)? If it works, can you please send me the link where i can find the Best practices Building Bl

  • Merge Query Question

    Ok, I have a Table TEST_ACCOUNTS that has all new customer information of new applicants for credit cards. Because of a Bank rules, customers are not assigned a customer_id number right away, takes about a month, so in this table I will have some CUS