Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7875635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:01:02+00:00 2026-06-03T03:01:02+00:00

I have a datetime field in a SQL Server 2005 table that has values

  • 0

I have a datetime field in a SQL Server 2005 table that has values like this:

2012-04-23 09:00:00.000
2012-04-23 14:00:00.000

The minutes, seconds, and microseconds are always zero.

I need to display a “time slot” (basically, the time plus one hour) like this:

2012/04/23  09:00 AM - 10:00 AM
2012/04/23  02:00 PM - 03:00 PM

I got what I needed using this:

SELECT SUBSTRING(CONVERT(VARCHAR, TimeSlot, 20), 1, 10) + '  ' +
       RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), TimeSlot, 109), 13, 5)), 5) + ' ' +
       SUBSTRING(CONVERT(VARCHAR(19), TimeSlot, 100),18,2) + ' - ' +
       RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), DATEADD(hh, 1, TimeSlot), 109), 13, 5)), 5) + ' ' +
       SUBSTRING(CONVERT(VARCHAR(19), DATEADD(hh, 1, TimeSlot), 100), 18, 2) AS TimeSlot
FROM MyTable

It works, but I feel so dirty.

I know I could do it easier in the code (VB .NET), but assume I have to do it in SQL.

Is there any cleaner way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:01:02+00:00Added an answer on June 3, 2026 at 3:01 am

    Taking the built-in conversions, I would use something like this:

    SELECT 
        convert(varchar, [TimeSlot], 111) + ' ' +
        RIGHT(convert(varchar, [TimeSlot], 0), 7) + ' - ' +
        RIGHT(convert(varchar, dateadd(hour, 1, [TimeSlot]), 0), 7)
    FROM
        [MyTable];
    

    this gives you the line you want as

    2012/04/23  9:00AM - 10:00AM
    

    in detail:

    • convert(varchar, @dt, 111) converts the datetime to the Japanese format yy/mm/dd
    • convert(varchar, @dt, 0) converts the datetime to Apr 23 2012 9:00AM so we can use the time part
    • dateadd(hour, 1, @dt) adds one hour to the current datetime value

    you can test it without tables with:

    DECLARE @dt DATETIME
    SET @dt = '2012-04-23 09:00:00'
    
    PRINT convert(varchar, @dt, 111) + ' ' +
          RIGHT(convert(varchar, @dt, 0), 7) + ' - ' +
          RIGHT(convert(varchar, dateadd(hour, 1, @dt), 0), 7);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2005 express I have a datetime field in a table
I have a table in SQL Server 2005 which has a date time field.
I have a table in SQL server which has a DATETIME field called Date_Printed.
Using SQL Server 2005 I have a field that contains a datetime value. What
If I have a datetime field (like Birthdate) in Sql Server, and I want
I have a few SQL Server tables with a datetime field that really only
I have bulk records that are getting imported into a SQL Server 2005 table.
I have a table in SQL Server 2005 which has three columns: id (int),
i have a sql server table with records (that isn't new) it has 1
In a SQL Server table, I have a DateTime field and every time I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.