Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Split date and time

Posted on 2014-03-22
11
Medium Priority
?
283 Views
Last Modified: 2014-07-05
I would like to be able to split a date field ActualDateTime to show date in one field and time in another field.

Field name is ActualDateTime
Table name is Travel

the date field will be Travel_Date
the time field will be Travel_Time . note that time would not be 24 hours.
0
Comment
Question by:Eddy2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39947518
-- To create the columns
ALTER TABLE Travel
ADD Travel_Date date, Travel_Time time
GO

-- To populate them based on your requirements
UPDATE Travel
SET Travel_Date = CAST(ActualDateTime as date), 
   Travel_Time = CAST(ActualDateTime as time) 
GO

Open in new window

0
 

Author Comment

by:Eddy2010
ID: 39947527
Thank you, this is a query only. I dont want to change the table structure. I want the output like that.
0
 

Author Comment

by:Eddy2010
ID: 39947531
I am not an administrator. I just want the query result to return the value.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 39947533
>I want the output like that.
In that case it's a SELECT query, which is below plus whatever you want to add to it.

SELECT 
   CAST(ActualDateTime as date) as Travel_Date, 
   CAST(ActualDateTime as time) as Travel_Time 
FROM Travel 

Open in new window

0
 

Author Comment

by:Eddy2010
ID: 39947535
thank you, but got the following error

Msg 243, Level 16, State 1, Line 3
Type date is not a defined system type.
Msg 243, Level 16, State 1, Line 3
Type time is not a defined system type.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39947542
What version of SQL Server are you running?  
SQL 2005 only had the datetime data type, which is both date and time.
SQL 2008 introduced separate date and time data types.
0
 

Author Comment

by:Eddy2010
ID: 39947543
2005
0
 

Author Comment

by:Eddy2010
ID: 39947546
sorry
2008 R2
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39947558
Assuming 2005, your only option here is to convert the date to a varchar, and then parse the string.  

The below works on my 2012 box, as I don't have 2005 handy.
Any date-specific server settings may affect the result.

CREATE TABLE Travel (ActualDateTime datetime) 
INSERT INTO Travel (ActualDateTime) 
VALUES ('2014-03-22 10:38:43.283'), ('2014-03-11 12:34:56.789'), ('2013-07-30 20:38:43.283') 

SELECT 
   convert(varchar, ActualDateTime ,5) as Travel_Date, 
   convert(varchar, ActualDateTime ,8) as Travel_Time 
FROM Travel

Open in new window


For more info on date styles, see SQL expert PortletPaul's article on SQL Server Date Styles (formats) using CONVERT()
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39948644
sorry
2008 R2

Since date and time are supported in SQL Server 2008, then perhaps that means your database is set to a compatibility level of 90 (SQL Server 2005)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39950875
CONVERT is the standard method of formatting a datetime for output.  In this specific case:


SELECT
    CONVERT(varchar(10), t.ActualDateTime, 101) AS Travel_Date,
    RIGHT(CONVERT(varchar(30), t.ActualDateTime, 0), 7) AS Travel_Time
FROM Travel t
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question