December 13, 2013 at 9:17 am
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
December 13, 2013 at 9:20 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
December 13, 2013 at 9:21 am
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
December 13, 2013 at 9:23 am
didn't notice this was for SQL2005, so the TIME datatype is out, but the other two converts i posted will work.
Lowell
December 13, 2013 at 9:29 am
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
December 13, 2013 at 11:18 am
Thanks Lowel (and everyone else who responded), very helpful.
Is there a way I can display this time in USD format?
December 13, 2013 at 12:28 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
December 13, 2013 at 12:49 pm
LOL, sorry, I meant 12 hour clock instead of a 24 hour clock.
Thanks
December 13, 2013 at 12:54 pm
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
December 13, 2013 at 12:57 pm
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
January 16, 2019 at 7:30 pm
SELECT CONVERT(DATE, CURRENT_TIMESTAMP)
declare @time as time
set @time = CONVERT(Time, CURRENT_TIMESTAMP)
SELECT @time
Smile Please
January 16, 2019 at 9:22 pm
sqlservercentral 98941 - Wednesday, January 16, 2019 7:30 PMSELECT CONVERT(DATE, CURRENT_TIMESTAMP)
declare @time as time
set @time = CONVERT(Time, CURRENT_TIMESTAMP)
SELECT @time
Realize that this is a 6 year old thread and the version of SQL Server is SQL Server 2005. The DATE and TIME data types are not available.
January 18, 2019 at 1:49 am
DaveDB - Friday, December 13, 2013 9:17 AMHello,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
declare @now datetime
set @now = getdate()
select dateadd(d,-datediff(d,0,@now),@now) as TimeOnly,
right(convert(varchar, dateadd(d,-datediff(d,0,@now),@now),22),11) as TimeOnly_US12h
January 18, 2019 at 2:26 am
You'll need to use the DATEPART function and concatenate the hour, minutes and seconds, like this:
declare @now datetime
set @now = getdate()
select CAST(datepart(hh,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2)) + ':' + CAST(datepart(mi,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
+ ':' + CAST(datepart(ss,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
as TimeOnly
Edit: In your case, the @now variable would be replaced by whatever column contains the date you want to use.
Probably others ways to do it, but the above works.
Lempster
January 18, 2019 at 9:38 am
Lempster - Friday, January 18, 2019 2:26 AMYou'll need to use the DATEPART function and concatenate the hour, minutes and seconds, like this:
declare @now datetime
set @now = getdate()
select CAST(datepart(hh,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2)) + ':' + CAST(datepart(mi,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
+ ':' + CAST(datepart(ss,dateadd(d,-datediff(d,0,@now),@now)) as varchar(2))
as TimeOnly
Edit: In your case, the @now variable would be replaced by whatever column contains the date you want to use.
Probably others ways to do it, but the above works.Lempster
Really? What about this:SELECT CONVERT(varchar(10),GETDATE(),108);
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply