How to split the date time field into two?

  • Hi team,

    I have a date time column and it show as follows " 2014-03-26 10:55". I want to write an expression to split this into columns, so that the date is separate from time.

    i/p is Datetime column - "2014-03-26 10:55"

    o/p is date column - 2014-03-26 and time column- 10:55 and it should be in 24 hr format.

    could some one help me with the logic.

    Thanks.

  • You can use CONVERT for that.

    DECLARE @TestDatetime DATETIME = GETDATE();

    DECLARE @TestDate DATE;

    DECLARE @TestTime TIME;

    SELECT @TestDate = CONVERT(DATE,@TestDatetime);

    SELECT @TestTime = CONVERT(TIME,@TestDatetime);

    PRINT @TestDate;

    PRINT @TestTime;

    If you want the values inserted in seperate columns, you can let SQL Server do the heavy lifting:

    CREATE TABLE #Test(TestDate DATE, TestTime TIME);

    INSERT INTO #Test(TestDate,TestTime) VALUES (GETDATE(),GETDATE());

    SELECT * FROM #Test;

    edit: I noticed after posting this that your question was in a Reporting Services forum. If you use SQL Server as a source, you can do this with the TSQL I posted. If not, you can use the FORMAT function in SSRS.

    Here's a nice article explaining all about it:

    Formatting Dates [SSRS][/url]

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 2 posts - 1 through 1 (of 1 total)

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