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
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.
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With