Remove DATE ONLY from datetime column

  • Hello,

    I need to remove DATE from a date time column in SQL 2005? All I keep finding on Google is how to remove the DATE, which I already know how to do, so how do I just display the time?

    Any help would be appreciated.

    Dave

  • DaveDB (12/13/2013)


    Hello,

    I need to remove DATE from a date time column in SQL 2005? All I keep finding on Google is how to remove the DATE, which I already know how to do, so how do I just display the time?

    Any help would be appreciated.

    Dave

    Honestly any and all formatted should be done in the front end. However, to do this in sql you can use CONVERT.

    http://msdn.microsoft.com/en-us/library/ms187928.aspx

    If you don't find a format you like you may have to use DATEPART to get the pieces you want in the format you want.

    _______________________________________________________________

    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/

  • Dave i think the easiest is cast your datetime as TIME datatype, but here's a quick example of the three i could think of:

    /*

    create_date TheTime CharTime108 CharTime114

    ----------------------- ---------------- ------------------------------ ------------------------------

    2010-03-29 08:55:29.650 08:55:29.6500000 08:55:29 08:55:29:650

    2005-10-14 01:36:15.910 01:36:15.9100000 01:36:15 01:36:15:910

    */

    with mycte

    AS

    (

    select top 10 create_date from sys.objects

    )

    select create_date,

    convert(time,create_date) as TheTime,

    CONVERT(varchar,create_date,108) As CharTime108,

    CONVERT(varchar,create_date,114) As CharTime114

    from Mycte

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • didn't notice this was for SQL2005, so the TIME datatype is out, but the other two converts i posted will work.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Keep in mind there is ALWAYS a date in the datetime datatype, you can't get rid of it, you can chose to not display it but it is and always will be there.

    CEWII

  • Thanks Lowel (and everyone else who responded), very helpful.

    Is there a way I can display this time in USD format?

  • DaveDB (12/13/2013)


    Thanks Lowel (and everyone else who responded), very helpful.

    Is there a way I can display this time in USD format?

    As in a a dollar sign and 2 decimal places? 😀

    What is the format you are looking for? We can help you find a way to do it.

    _______________________________________________________________

    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/

  • LOL, sorry, I meant 12 hour clock instead of a 24 hour clock.

    Thanks

  • You just have to play with format codes 🙂

    SELECT TOP 100

    SUBSTRING( CONVERT(varchar,create_date,109), 13, 14) As CharTime

    FROM sys.objects

    REFERENCE: http://msdn.microsoft.com/en-us/library/ms187928.aspx

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • assuming you want AM/PM? here's a couple more examples; one keeps the milliseconds, the other is stuffed to remove the milliseconds to leave seconds +am/pm:

    select right(convert(varchar,getdate(),131),14),

    STUFF(right(convert(varchar,getdate(),131),14),9,4,'')

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • SELECT CONVERT(DATE, CURRENT_TIMESTAMP)
    declare @time as time
    set @time = CONVERT(Time, CURRENT_TIMESTAMP)
    SELECT @time

    Smile Please
  • sqlservercentral 98941 - Wednesday, January 16, 2019 7:30 PM

    SELECT CONVERT(DATE, CURRENT_TIMESTAMP)
    declare @time as time
    set @time = CONVERT(Time, CURRENT_TIMESTAMP)
    SELECT @time

    Realize that this is a 6 year old thread and the version of SQL Server is SQL Server 2005.  The DATE and TIME data types are not available.

  • DaveDB - Friday, December 13, 2013 9:17 AM

    Hello,I need to remove DATE from a date time column in SQL 2005? All I keep finding on Google is how to remove the DATE, which I already know how to do, so how do I just display the time?Any help would be appreciated.Dave

    declare @now datetime
    set @now = getdate()
    select dateadd(d,-datediff(d,0,@now),@now) as TimeOnly,
        right(convert(varchar, dateadd(d,-datediff(d,0,@now),@now),22),11) as TimeOnly_US12h

  • You'll need to use the DATEPART function and concatenate the hour, minutes and seconds, like this:


    declare @now datetime
    set @now = getdate()
    select CAST(datepart(hh,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2)) + ':' + CAST(datepart(mi,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
    + ':' + CAST(datepart(ss,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
    as TimeOnly

    Edit: In your case, the @now variable would be replaced by whatever column contains the date you want to use.
    Probably others ways to do it, but the above works.

    Lempster

  • Lempster - Friday, January 18, 2019 2:26 AM

    You'll need to use the DATEPART function and concatenate the hour, minutes and seconds, like this:


    declare @now datetime
    set @now = getdate()
    select CAST(datepart(hh,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2)) + ':' + CAST(datepart(mi,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
    + ':' + CAST(datepart(ss,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
    as TimeOnly

    Edit: In your case, the @now variable would be replaced by whatever column contains the date you want to use.
    Probably others ways to do it, but the above works.

    Lempster

    Really?  What about this:
    SELECT CONVERT(varchar(10),GETDATE(),108);

Viewing 15 posts - 1 through 15 (of 22 total)

You must be logged in to reply to this topic. Login to reply