[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

t-SQL format time and combine columns

Posted on 2011-09-19
5
Medium Priority
?
264 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
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

829 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