Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date representing the first day of a month?

I need functionality in a script that will enable me to insert dates into a table.

What SQL do I need to insert a date of the format

01/08/2010 00:00:00 

where the date is the first day of the current month. What do I need to change order that I can specify the month value? Thanks

like image 626
DaveDev Avatar asked Aug 17 '10 14:08

DaveDev


People also ask

How do you convert a date to the first day of the month?

Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month. To perform the previous example with the EOMONTH function, we need to use the formula =EOMONTH(A2,-1)+1 in cell B2.

How do I get the first day of a month in SQL?

Here's how this works: First we format the date in YYYYMMDD... format truncating to keep just the 6 leftmost characters in order to keep just the YYYYMM portion, and then append '01' as the month - and voila! you have the first day of the current month.

How do you show the first day of the month in Excel?

So, in this formula EOMONTH first returns a date corresponding to the last day of the month, and then DAY returns the date... Working from the inside out, the EOMONTH function gets the last day of month of any date. To this result, we add 1, which results in the first day of the next month.

What goes first day of month?

The day is written first and the year last in most countries (dd-mm-yyyy) and some nations, such as Iran, Korea, and China, write the year first and the day last (yyyy-mm-dd). But why did Americans choose to write the month first?


1 Answers

The best and easiest way to do this is to use:

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

Just replace GETDATE() with whatever date you need.

like image 63
Icemanind Avatar answered Sep 30 '22 09:09

Icemanind