SQL if last day of pervious month has value change the beginning date of next month to start from that day - sql

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks

I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

Related

How to add/subtract weeks to the third day of the week?

I'm trying to figure out how to translate this line of PROC SQL code into Snowflake SQL but haven't found a way yet.
%LET last_post_dt = %SYSFUNC(INTNX(WEEK.3,%SYSFUNC(TODAY(),),-2,B),DATE9.);
Basically, its subtracting 2 weeks from the third day of the current week (monday = 1).
I've tried altering the session by using WEEK_START and other functions such as DATEADD, but haven't been able to solve this.
Thanks in advance!
You can use date_trun() to get whatever today is to a Monday. Then add 2 for 3rd day of the week, and then subtract 2 weeks from that.
SELECT DATE_TRUNC('week',current_date()) as week_start,
DATEADD('day',2,week_start) as week_3rd_day,
DATEADD('week',-2,week_3rd_day) as weeks_ago,
DATEADD('week',-2,DATEADD('day',2,DATE_TRUNC('week',current_date()))) as all_in_one_line_date

SQL Query that rund monthly on a fixed day range

I have an SQL query I need to run once a month.
The data set the query produces always has to be from the 11th of the month before to the 10th of the current month.
I now manualy run the query in the fews days after the 11th day of the month manually adjusting the date range in my where statement:
for example...
Where Column A is greater than 10/10/2015 and less than 12/11/15
I was hoping there would be a statement I could add to my query to automatically find the 11th day of the last month and the 10th of the current month. This way I could schedule the query and automatically email the results.
You should be able to use the following within your query: -
CONVERT(date,FORMAT(GETDATE(),'yyyy-MM')+'-10')
(for the 10th of this month)
and
CONVERT(date,FORMAT(DATEADD(m,-1,GETDATE()),'yyyy-MM')+'-11')
(for the 11th of last month).
Try to look out the MONTH() function in your working DBMS. In MySQL and MSSQL it returns a number (1 been january) corresponding to the current month that your system is (you may check if it's date is updated).
With this function you can subtract 1 to get the last month, having to do some logic when the current one is January, hence 1. Since now you should get 12 (december) intead of 0 (an error).
Cheers, mate!

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

How do I get the date of the last day of the previous month in the previous year in SQL?

I am writing a query with a where clause which needs to return the final day of the previous month in the previous year. So if I ran it today (18/06/2014), it would return 31/05/2013. If I ran it next month on 19/07/2014 it would return 30/06/2013 and so on.
I can use
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,GETDATE()),0))
which returns the final date of last month but I can't work out how to adapt this to give me the date a year previously.
I can add - 365 to the end of the above code which does what I want but wouldn't account for leap years. Although I don't expect my reports to be in use for long enough for this to necessarily matter it would be good to find a solution that works nicely (and to think that they might be).
Any solutions greatly appreciated.
Just add another dateadd(YY, -1, {your code}) around your getdate() to roll the year back first, then look to previous month (per comments below).
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,DATEADD(YY,-1,GETDATE())),0))

create week function, but instead of Sunday start day of the week, Monday

Anyone know of a script, that creates a week table, that is based on Monday as the start day of the week and not Sunday? For MS SQL
You can adjust the way SQL Server sets the first day of the week using http://msdn.microsoft.com/en-us/library/ms181598.aspx if that helps?
Basically this is putting every sunday to the week before, with week=0->week=52