Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:24 AM
Points: 15,806, Visits: 16,170
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: Today @ 9:20 AM
Points: 14,436, Visits: 37,830
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1522758
Posted Friday, December 13, 2013 9:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 14,436, Visits: 37,830
didn't notice this was for SQL2005, so the TIME datatype is out, but the other two converts i posted will work.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1522761
Posted Friday, December 13, 2013 9:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 17, 2015 11:50 AM
Points: 6,050, Visits: 5,314
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:24 AM
Points: 15,806, Visits: 16,170
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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 7,481, Visits: 16,231
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.
General Disclaimer:
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?


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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 14,436, Visits: 37,830
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1522852
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse