?
Solved

t-SQL format time and combine columns

Posted on 2011-09-19
5
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

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

Author Comment

by:cdemott33
ID: 36563211
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 2000 total points
ID: 36563343
SELECT SUBSTRING(CONVERT(varchar(20),StartTime,0),13,7) + ' - ' +  SUBSTRING(CONVERT(varchar(20),EndTime,0),13,7)
0
 
LVL 25

Expert Comment

by:jogos
ID: 36563355
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
ID: 36563380
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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