datetime field in SQL 2008

Greeting,

I have a datetime field name Ent_date with the following sample data in SQL 2008
2015-05-01 05:00:00.000

How to split it into the ent_date and ent_time with the following format in SQL.
01-MAY-15
05:00 PM

Thanks.
mrongAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
Declare @dt datetime ='2015-05-01 05:00:00.000';

Select cast(@dt as date), cast(@dt As time)

Open in new window


This produces 2015-05-01      05:00:00.0000000

The displays need to be modified, brb
grendel777Commented:
You can use the CONVERT function to format the date and time as strings, but note that in order to do date calculations you'll need to re-convert them into datetimes:

SELECT REPLACE(CONVERT(NVARCHAR, CAST('2015-05-01 05:00:00.000' AS DATE), 6), ' ', '-') AS StringDate,
    RIGHT(CONVERT(NVARCHAR, CAST('2015-05-01 05:00:00.000' AS DATETIME), 100), 7) AS StringTime

Open in new window


This site has a list of the different format options - I keep a printout on my wall for easy reference: http://www.sqlusa.com/bestpractices/datetimeconversion/
mrongAuthor Commented:
grendel777,

Tried your suggestion but got the following error message.
Type DATE is not a defined system type
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Mike EghtebasDatabase and Application DeveloperCommented:
Declare @dt datetime ='2015-05-01 05:00:00.000';
Select format(@dt,'dd-MMM-yy'), format(@dt,'hh:mm')

Open in new window


gives 01-May-15      05:00
mrongAuthor Commented:
grendel777,

I changed DATE to DATETIME and it gives what I need.
The time out put like "05:00PM". Is there anyway to make it "05:00 PM" with an extra space?
Thanks.
Mike EghtebasDatabase and Application DeveloperCommented:
Declare @dt datetime ='2015-05-01 05:00:00.000';
Select format(@dt,'dd-MMM-yy'), format(@dt,'hh:mm') + ' ' + RIGHT(CONVERT(VARCHAR(30), @dt, 9), 2) 

Open in new window


returns
01-May-15      05:00 AM
Jim HornMicrosoft SQL Server Data DudeCommented:
>Type DATE is not a defined system type
Any chance this database is really 2005 or earlier?  The date data type was introduced in 2008.

Also, SQL expert Paul Maxwell has an article SQL Server Date Styles (formats) using CONVERT() that is an excellent date format reference.
grendel777Commented:
That's weird, DATE should be a valid data type in 2008 (unless that's only in 2008 R2?). This will add the space:

SELECT REPLACE(CONVERT(NVARCHAR, CAST('2015-05-01 05:00:00.000' AS DATETIME), 6), ' ', '-') AS StringDate,
	REPLACE(REPLACE(RIGHT(CONVERT(NVARCHAR, CAST('2015-05-01 05:00:00.000' AS DATETIME), 100), 7), 'AM', ' AM'), 'PM', ' PM') AS StringTime

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
< very wild guess that's out of my skillsets >  What is this databases' compatibility level set to?  If it's 2005, that may prevent code execution that include data types introduced after 2005.

Source / inspiration for asking...
Vitor MontalvãoMSSQL Senior EngineerCommented:
mrong, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.