Get start and end date by month name, not month number

theLuckyOne

How do I get start date and end date of a month by month name? Not by date, not by month number.

For example, this gets me first day of current month. Input is a date type, not string like I want it to be.

SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)

What I want is to input month name in string like 'March' not 3 and get 2016/03/01 and 2016/03/31. I'd really be overwhelmed and appreciate any/all help.

Felix Pamittan

You can create a CTE to get the month number:

DECLARE @month VARCHAR(10) = 'March';

WITH CteMonths(n, m) AS(
    SELECT 1, 'January' UNION ALL
    SELECT 2, 'February' UNION ALL
    SELECT 3, 'March' UNION ALL
    SELECT 4, 'April' UNION ALL
    SELECT 5, 'May' UNION ALL
    SELECT 6, 'June' UNION ALL
    SELECT 7, 'July' UNION ALL
    SELECT 8, 'August' UNION ALL
    SELECT 9, 'September' UNION ALL
    SELECT 10, 'October' UNION ALL
    SELECT 11, 'November' UNION ALL
    SELECT 12, 'December'
)
SELECT
    DATEADD(MONTH, n - 1, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)),
    DATEADD(DAY, -1, DATEADD(MONTH, n, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)))
FROM CteMonths
WHERE m = @month

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

get start and end date of month by week number of the month in php

How pass month value and get start and end date of month In laravel

Get month name with different month start and end dates

How to get the start date and end date when the year number and month number are known in php

unstack start and end date to month

Start and end date of a current month

Customised start and end date of the month

Get start and end date of a week. Month, week number, and year is given as user input

How to get Start Date and End Date in Last month using Javascripts?

Get Start Date and End date for certain month count

how to get start date and end date of previous month in swift

How to get start date and end date of the current month (Swift 3)

Get monthly array with start date and end date of month in php

Calculate start and end date for the given week number in particular month in angular

How to get the start and the end of date for each month with NaiveDate (Rust)?

get start and end day of week by year, month and week number

get Month start date and end date of all month passed between 2 Dates in python or Mysql

Get month number from month name

Get Month name from month number

MYSQL - Get Month Number from Month Name

Given start date get all the months name until current month?

Get month name from Date

Get month name from number

How to get month name by number

Shift the business month end date by different number of month in pandas

splitting start and end date to start_date / first day of month and end_date / last day of month

Calculate number of workdays PER MONTH from start_date and end_date

Get a range of months using moment with start month and end month

Get start & end timestamp for month from Month and Year