-2

I have a column called enteredindate that includes data such as:

2012-10-10 18:02:00
2012-10-11 13:30:00
2012-10-11 14:27:00
2012-10-12 14:14:00

How do I make a new column that has an INT data value next to this enteredindatecolumn like this?

1802
1330
1427
1414
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • (Parse out the Hour) * 100 + Minute [DatePart](http://msdn.microsoft.com/en-us/library/ms174420%28v=sql.110%29.aspx) – Adam Wenger Oct 06 '14 at 18:59
  • 4
    So, as @AdamWenger said: `DATEPART(HOUR,enteredindate)*100+DATEPART(MINUTE,enteredindate)` – Lamak Oct 06 '14 at 19:00
  • 1
    The [Extracting hours from a DateTime (SQL Server 2005)](http://stackoverflow.com/q/1114307/335858) question suggested as a duplicate is not a good candidate, because it answers only part of the question (although it does give a good idea on how to proceed). The second part, namely, how to combine the hours and minutes, is not answered in the duplicate. I am voting to re-open this question (but please comment if you find a better duplicate, I'll gladly vote to close the question back). – Sergey Kalinichenko Oct 06 '14 at 21:18

2 Answers2

2

Use datepart:

select datepart(hour, enteredindate) * 100
       + datepart(minute, enteredindate)
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

You can compute the desited number using this expression:

DATEPART(hour,d) * 100 + DATEPART(minute,d)

where d is the name of your datetime column.

Multiply hours by 100 before adding minutes to treat them as the first two digits of a four-digit number.

Demo.

Zane
  • 4,129
  • 1
  • 21
  • 35
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523