Solved

t-SQL format time and combine columns

Posted on 2011-09-19
5
238 Views
Last Modified: 2012-05-12
Hi All -

I have two columns... timeStart and timeEnd.  They contain the following values

timeStart = 1/1/1900 8:00 AM
timeEnd = 1/1/1900 5:00 PM

I need to cut the date out of the columns and combine the two of them into a new column called timeSpan, which would return this...

8:00 AM - 5:00 PM

I tried this....

timeStart + '-' + timeEnd As timeSpan

... but it doesn't work.  Can someone show me how I can get the result I'm looking for.

Thanks!
0
Comment
Question by:cdemott33
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:lludden
Comment Utility
SELECT CONVERT(varchar(5),TimeStart,108) + ' - ' + CONVERT(varchar(5),TimeEnd,108)
0
 

Author Comment

by:cdemott33
Comment Utility
Hi lludden - Thanks for your help.  Your code returned this value...

08:00 - 17:00

Is there a way to convert it from military time to standard time and add the AM/PM?
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
Comment Utility
SELECT SUBSTRING(CONVERT(varchar(20),StartTime,0),13,7) + ' - ' +  SUBSTRING(CONVERT(varchar(20),EndTime,0),13,7)
0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
The complete syntax  can you find on http://msdn.microsoft.com/en-us/library/ms187928.aspx and http://msdn.microsoft.com/en-us/library/ms187748.aspx and http://msdn.microsoft.com/en-us/library/ms186323.aspx
Convert your date to a varchar (CONVERT), take a substring (SUBSTRING) from the time-part and that begins after the first space(CHARINDEX).

You can also use the DATEPART function http://msdn.microsoft.com/en-us/library/ms174420.aspx

datepart(hour,timeStart)+':'datepart(minute,timeStart) ....but then the AMPM becomes difficult.

0
 

Author Closing Comment

by:cdemott33
Comment Utility
This worked.  Thank you!  I also made a few changes to your code based on some research I was doing and came up with this...

CONVERT(varchar(15), CAST(timeStart As Time), 100) + ' - ' + CONVERT(varchar(15), CAST(timeEnd As Time), 100) as timeSpan,

Which also works.  Thanks again everone who helped me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now