Home Forums SQL Server 2008 T-SQL (SS2K8) varchar to time or datetime or ARGGHH Please help me. RE: varchar to time or datetime or ARGGHH Please help me.

  • nicoleaslater (9/26/2012)


    This might make more sense. Formatting is never my thing.

    check_in

    1911-03-02 14:00:00.000

    1911-03-02 14:00:00.000

    appt_time

    1300

    0950

    No you are not asking the impossible but you did not post this like I suggested (ddl and sample data). In order to make this work we need something that we can copy and paste into SSMS so we can work on your problem instead of spending time setting up the problem. I know you are new around here so I did this for you as an example.

    Here is your data is an easily consumable format.

    create table #CheckIn

    (

    check_in datetime,

    appt_time char(4)

    )

    insert #CheckIn

    select '1911-03-02T14:00:00.000', '1300' union all

    select '1911-03-02T14:00:00.000', '0950'

    select *

    from #CheckIn

    The advantage is two fold. The volunteers that are going to help with your issue don't have to write this first. Instead they can spend their time working on the issue at hand with little setup effort. Secondly, there is no doubt about datatype and such.

    OK so what so I demonstrated how you should post questions in the future. What you really care about is some help with your solution.

    Try this.

    ;with cte as

    (

    select check_in, appt_time, dateadd(n, CAST(right(appt_time, 2) as int), dateadd(hh, cast(left(appt_time, 2) as INT), dateadd(dd, datediff(dd, 0, check_in), 0))) as AppointmentTime

    from #CheckIn

    )

    select *, DATEDIFF(n, check_in, AppointmentTime) from cte

    I think that is what you are looking for, or at least it is reasonably close.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/