SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to split the date time field into two?


How to split the date time field into two?

Author
Message
kish1234
kish1234
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 98
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.
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)SSC Guru (110K reputation)

Group: General Forum Members
Points: 110544 Visits: 13338
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]


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search