question

cbrown197 avatar image
cbrown197 asked

Problem with Date Math

Hi SQL Gurus, I can't seem to find the answer to this simple problem. I am trying to convert a datetime to a numeric(5,2). I only want the minutes converted. Here is my date:1899-12-30 08:30:00.000 Here is my attempt: CAST(DATEPART(minute, HC_ApptDetail.TimeFrom) / 60 AS numeric(5, 2)) AS starttimeM_t The result: 0.00 it should be 0.50 What am I missing?
convertrounddatepartcast
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Tom Staab avatar image
Tom Staab answered
Even though you are later casting to a decimal type, SQL Server is returning an integer from your division because both the numerator and denominator are integers. Then it is simply casting that integer to your desired data type. If you change "60" to "60.0", you will get the answer you want.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks Tom!
0 Likes 0 ·
If Tom's answer helped, be sure to vote for it and/or accept it.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.