Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remove DATE ONLY from datetime column Expand / Collapse
Author
Message
Posted Friday, December 13, 2013 9:17 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, Visits: 215
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
Post #1522751
Posted Friday, December 13, 2013 9:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:03 PM
Points: 13,289, Visits: 12,136
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1522756
Posted Friday, December 13, 2013 9:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:48 PM
Points: 12,908, Visits: 32,005
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1522758
Posted Friday, December 13, 2013 9:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:48 PM
Points: 12,908, Visits: 32,005
didn't notice this was for SQL2005, so the TIME datatype is out, but the other two converts i posted will work.

Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1522761
Posted Friday, December 13, 2013 9:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
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
Post #1522766
Posted Friday, December 13, 2013 11:18 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, Visits: 215
Thanks Lowel (and everyone else who responded), very helpful.

Is there a way I can display this time in USD format?
Post #1522812
Posted Friday, December 13, 2013 12:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:03 PM
Points: 13,289, Visits: 12,136
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1522836
Posted Friday, December 13, 2013 12:49 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, Visits: 215
LOL, sorry, I meant 12 hour clock instead of a 24 hour clock.

Thanks
Post #1522849
Posted Friday, December 13, 2013 12:54 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:16 PM
Points: 3,535, Visits: 7,613
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1522851
Posted Friday, December 13, 2013 12:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:48 PM
Points: 12,908, Visits: 32,005
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1522852
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse