Reputation: 77
I have many varchar
YYYY-mm values in my table :
But now I need days in Month from this.
I need the result: 2018-04 -> 30 Days
So how I can achieve this?
I thank you in advance!
Upvotes: 1
Views: 448
Reputation: 522762
In addition to Gordon's spot-on answer, here is another way to compute the number of days in the month:
SELECT
DATEDIFF(day,
TRY_CONVERT(datetime, date+'01'),
DATEADD(month, 1, TRY_CONVERT(datetime, date+'01')))
FROM yourTable;
Actually, if you have a long term need for the number of days in each calendar month over some range, you might want to just create a calendar table with this fixed table. Then, you may join to it whenever you need it.
Upvotes: 1
Reputation: 1271151
You can use eomonth()
:
select day(eomonth(convert(date, yyyymm + '-01')))
Here is a db<>fiddle for doubters.
Upvotes: 4