Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: How to get time from datetime

  1. #1
    Join Date
    Apr 2003
    Posts
    11

    Question How to get time from datetime

    Sorry if this has been asked befor, but I couldn't find anything that hepled me.

    I have a datetime field and want to extract only the time. I know it works with CONVERT and style-type 108. But I get something like this: 08:00:00 and I need something like this: 08:00.

    Can this somehow be done?

    Michael

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    select left(convert(varchar(10),getdate(),108),5)

  3. #3
    Join Date
    Apr 2003
    Posts
    11

    universal format for datetime insert?

    Originally posted by MAK
    select left(convert(varchar(10),getdate(),108),5)
    Great! Thanks a lot. I have one other problem, maybee you can help me here too:

    Depending on what version (German, English etc) of MS SQL Serve you are using it needs other formats when you want to put a date AND time into a datetime column.
    Is there a universal format that every MS SQL Server accepts?

    Michael
    Last edited by roj2003; 04-08-2003 at 04:54 AM.

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    You can use cast or convert to change style, see 'cast and convert' in books online for details.

  5. #5
    Join Date
    Apr 2003
    Posts
    8

    similar problem and I'm a newby

    I have a similar issue but am new to this so please go easy.
    I have a db datetime field. With a stored proc using a view I want to convert the datetime to be just the date, no time returned at all.

    I have the date field in the view but an not sure of the syntax.

    Please help?

    Thanks in advance,
    Whack

  6. #6
    Join Date
    Sep 2002
    Posts
    5,938
    Try

    select convert(varchar(10),getdate(),110)

  7. #7
    Join Date
    Apr 2003
    Posts
    8

    no luck yet

    here is the date in the table; 2003-02-04 00:00:00.000.
    Here is the part of the View that I need the conversion to work in (updated with your suggestion)...convert(varchar(10), dbo.[Order].manual_promise_date,110) AS ManualPromiseDate.
    And this is what gets sent back...2003-02-04T00:00:00.0000000-05:00 when I want just 2003-02-04.

    Thanks in advance.

  8. #8
    Join Date
    Sep 2002
    Posts
    5,938
    What's data type of manual_promise_date? Tried on datetime column and the code worked.

  9. #9
    Join Date
    Apr 2003
    Posts
    8

    Promise date is a datetime column 8 allows nulls

    My view code is as follows...

    convert(varchar(10), dbo.[Order].manual_promise_date,110) AS ManualPromiseDate

    any ideas or articles. Must be something I'm not getting. I even tried to change the 110 to something else but I keep getting the date in the following format.

    <PromiseDate>2003-02-06T00:00:00.0000000-05:00</PromiseDate>

    Pulling out what little hair I have left.....

  10. #10
    Join Date
    Sep 2002
    Posts
    5,938
    Look at order table to find out data type of manual_promise_date. Is it datetime or char?

  11. #11
    Join Date
    Apr 2003
    Posts
    8
    sorry I put the datatype in the subject line on the last post.
    It is datetime 8 and allows nulls for what that is worth.

  12. #12
    Join Date
    Sep 2002
    Posts
    5,938
    Try following in query analyzer to see what do you get:

    select convert(varchar(10), manual_promise_date,110) AS ManualPromiseDate from order ...

  13. #13
    Join Date
    Apr 2003
    Posts
    8
    It works in query analyizer but when I put that syntax into the view I get an error near the word Select. I am already doing a distinct select so I dropped the select.
    Should I handle the select differently?
    I will attach the complete view.

    Thanks so much for all the help.
    Attached Files Attached Files

  14. #14
    Join Date
    Sep 2002
    Posts
    5,938
    I just created a view with convert and works again. What's your sql version by the way?

  15. #15
    Join Date
    Apr 2003
    Posts
    8
    Version of SQL 2000 is 8.00.194.

    Can you please post your view syntax.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •