Substring issue

  • Hi, I want to return only the hour of a time ie 17:00 should return 17

    The code below selects the time from a date and then rounds up or down to the nearest hour.

    SELECT CAST(DATEADD(hour,DATEDIFF(hour,0,DATEADD(minute,30,GETDATE())),0) AS TIME(0))

    I tried to add the SUBSTRING function to this but to no avail.

    Any suggestions?

    Thanks!!

  • convert the date right away to time, and then you are working with hours from 0 to 23:59:59;

    so when you round the TIMe, it's between 0 and 23:

    --returns 10 AS INTEGER

    SELECT DATEDIFF(hour,0,DATEADD(minute,30,convert(time,GETDATE())))

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Perfect! Thank-you for your prompt response 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply