Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to separate time and date in sqlserver 2005

its displaying in my table as 16012014 081500

Just i want to dispaly only time as 08:15 using sql query

Please help thanks.
Posted

You can do
SQL
select Convert(TIME, yourDateTimeColumn) as TheTime


Oops - the poster below is right - instead you can use

SQL
select Convert(VARCHAR(8),yourDateTimeColumn,108) as theTime


sorry.
 
Share this answer
 
v2
Comments
Ankur\m/ 16-Jan-14 1:15am    
AFAIK Time field was introduced in SQL Server 2008. So that won't work in 2005.
Ankur\m/ 16-Jan-14 3:09am    
Voting a 4 as you have corrected the answer and it will also counter the unnecessary down vote.
Try to use datepart function where first u have to get hour and then minute and concat this in query e.g select DATEPART(hh,OrderDate) + ":" + DATEPART(mm,OrderDate) like this
Also you can use convert function
SQL
CONVERT(VARCHAR(24),GETDATE(),108)

but here u get seconds also
 
Share this answer
 
v2
16012014 081500
That isn't correct date time format.
For a correct format you can do something like below:

SQL
DECLARE @SomeDateTime datetime
SET @SomeDateTime = '2014-01-16 08:15:00'

SELECT CONVERT(CHAR(5), @SomeDateTime, 108) [time]
 
Share this answer
 
SQL
DECLARE @SomeDateTime datetime
SET @SomeDateTime = '2014-01-16 08:15:00'

SELECT CONVERT(CHAR(8), @SomeDateTime, 108) [Time]

SELECT CONVERT(CHAR(8), @SomeDateTime, 103) [Date] 
 
Share this answer
 
i agreed with ankur. '16012014 081500' is not correct format.

with correct format like '2014-01-16 08:15:00' you can write below query:-
SQL
DECLARE @d datetime
SET @d = '2014-01-16 08:15:00'
SELECT
CONVERT(VARCHAR(8),@d,108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),@d,101) AS DateOnly
 
Share this answer
 
So your data is not in a correct datetime format then you can use something like

SQL
declare @item varchar(20)
set @item='16012014 081500';
select STUFF((SUBSTRING(@item,9,5)), 4, 0, ':')


it return exactly what you want i.e. 08:15
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900