I have small table
Create Table TestDates
(
TestDateId int identity (1,1),
RunDateTime DateTime
)
I manage to execute distinct query that brings only time but
if hour or minute is less then 10 I want to add zero
SELECT DISTINCT SUBSTRING(CONVERT(varchar, RunDateTime, 100), 13, 2) + ':'
+ SUBSTRING(CONVERT(varchar, RunDateTime, 100), 16, 2) + ' '
+ SUBSTRING(CONVERT(varchar, RunDateTime, 100), 18, 2) AS DistinctDate
from TestDates
See example
**Current Output Desired Output**
8:1 PM **08:01 PM**
8:33 AM **08:33 AM**
I tried to use right function but it does not help
SELECT DISTINCT Right('00' + SUBSTRING(CONVERT(varchar, RunDateTime, 100), 13, 2),2) + ':'
+ Right('00' + SUBSTRING(CONVERT(varchar, RunDateTime, 100), 16, 2),2) + ' '
+ SUBSTRING(CONVERT(varchar, RunDateTime, 100), 18, 2) AS DistinctDate
from TestDates
Please advice
Try this:
The problem is that the first substring returns _8 (space 8), so you need to trim the value to get rid of the space, then do you adding 00 code.