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

  • SEARCH
  • Home
  • 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 7501705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:37:51+00:00 2026-05-29T20:37:51+00:00

CAST(DATEPART(hh, timestamp) AS varchar(2)) + ‘:00’ AS Hour This will get me the hour

  • 0
CAST(DATEPART(hh, timestamp) AS varchar(2)) + ':00' AS Hour

This will get me the hour out of a timestamp field but in the case of the hours 0-9 it does not pad a leading zero and therefore when I sort by hour descending it does not sort correctly.

Not sure what is wrong here. I specify a 2 char varchar to allow extra room for the leading zero. Hopefully there is a way to fix this without passing my field through a function that will pad the leading zero for me.

  • 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-05-29T20:37:52+00:00Added an answer on May 29, 2026 at 8:37 pm
    SELECT RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, GETDATE())), 2) + ':00';
    

    Don’t want to pad with 0s? OK, you can do it a similarly ugly way without the padding mess:

    SELECT LEFT(CONVERT(TIME(0), GETDATE()), 2) + ':00';
    

    Obviously replace GETDATE() with your column name. Which I hope isn’t really timestamp because this is a reserved word – for a data type that has nothing to do with date or time, sadly.

    If you don’t like either of those solutions, then just select the data from SQL Server and let your client application handle formatting/presentation details. Surely this is easy to do with C#’s format() function, for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am running this report: use SalesDWH go ;with cte_biggie as( select (c.npi+'|'+ CAST(DATEPART(yy,
CAST(IH.InvoiceDate AS date) AS InvoiceDate this returns 2012-03-16 is it possible to format this
Is it legal to cast this way? void probability(void **value) { double v =
I want to create indexed view with such t-sql: Select Table1_ID, cast(CONVERT(varchar(8), t2.Object_CreationDate, 112)AS
SELECT CAST(CONVERT(varchar, W.CreateTS, 101)AS SMALLDATETIME) AS [SoldDate] ,COUNT(*) AS NumberOfWidgets ,FT.FormName FROM tblWidget W
Select cast(ltrim(rtrim(Substring(string,charindex('my',string)+len('my')+5, charindex('for the company',string)-charindex('my',string)+len('my')-9))) as datetime) from table Select cast(ltrim(rtrim(Substring(string,charindex('company',string)+len('company')+1, len(String)-charindex('company',string)+len('company')-9)))as varchar) from
dynamic_cast throws bad_cast exception if you cast a reference but as I know in
dynamic_cast evaluates to NULL if the cast involves pointers, but throws a bad_cast exception
select cast(de.ApprovalOrder AS VARCHAR(32)) + cast(de.EntityCode AS VARCHAR(32)) + isnull(cast(de.DelegationCode AS VARCHAR(32)), '') as
SELECT YEAR(aum.AUM_Timeperiod) as Year, DATEPART(q, aum.AUM_TimePeriod) AS Quarter, SUM(cast(aum.AUM_AssetValue AS money)) as total_AssetValue FROM

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.