'Distinct Time from Table SQL
I have table Proccesses that consist of Date Time Field.
Proccesses ( Id as int , RunDate as DateTime) I need to run Distinct by RunDate as Time Without seconds
For Example
ID RunDate
1 2011-12-13 12:36:26.483
2 2011-12-12 12:37:22.421
3 2011-12-11 12:36:44.421
I need to receive in output
Output
12:01
12:03
In order to retrieve it I using following SQL and it's working
SELECT DISTINCT DATENAME(hour, RunDateTime) + ':' +
DATENAME(mi, RunDateTime) AS d
from Proccesses
The problem that if minutes is less than 10 for example 8 I receiving single digit "8" , and I want to receive two digit 08
For example I receive 12:8 , and I need to receive 12:08
Solution 1:[1]
CONVERT style 108 will return hh:mm:ss. Using CHAR(5) for the data type will return just the hh:mm portion.
SELECT DISTINCT CONVERT(CHAR(5), RunDateTime, 108) AS d
FROM Processes
Solution 2:[2]
SELECT RIGHT(CONVERT(VARCHAR,RunDateTime),7)
You could strip of the AM/PM if you want to.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joe Stefanelli |
| Solution 2 | syneptody |
