Date search range previous month to current month - sql-server

I had a working SQL date search...
(DATEADD (dd,-30, getdate()) <= pymnt_pstd_dt)
it gives me data from the past 30 days, but what I actually need is a comparison of the previous month MTD with the current month MTD
I am currently struggling with a good way to write that for SQL
Any help would be appreciated.

Here are WHERE statements that will get month to date and previous month to date for your field, pymnt_pstd_dt:
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE()),0) AND GETDATE() --Month to date
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE())-1,0) AND DATEADD(MONTH,-1,GETDATE()) --Previous month to date

Related

Create a rolled up week column using the day column and populate the value with first monday of the week: Snowflake database

Snowflake Database:
I have a day level table , i am trying use the day column to create a week column with the value of first Monday's of the week as a week value using following function. even though the table have a data for all seven days ( Monday through Sunday ) of the week. The following function will create a week column with five working days ( Monday through Friday) only rolled up to a week leaving Saturday and Sunday. is there any function which i can use to grab all seven days of data under one week in SNOWFLAKE DATABASE (first monday of the week).
CURRENT FUNCTION USED:
select dateadd('day', (extract('dayofweek_iso', current_date()) * -1) +1 , current_date() );
Snowflake provides DATE_TRUNC() to roll up date to corresponding year/month/week/day.
For the mentioned use case following query will roll up to Monday of the week.
select date_trunc('WEEK',current_date()) ;
Snowflake last_day() function can be used to achieve that. You can easily get the last day of week (Sunday) and subtract 6 days to get the first day of that week (Monday) like that:
select last_day(current_date(), 'week')-6 as first_day_of_week;

I want to calculate Week-Ending date for particular dates given in a separate column

I am currently working on SQL Server 2019. There is need to calculate week-ending date (every Sunday) for a given effective date in a column. I am trying to make it work using the below logic.
dateadd(dd,((datediff(dd,'17530107',GETDATE()+13)/7)*7)+7,'17530107') as Weekend_Date
Desired Output,
Effective Date ( this date is give): Week-Ending Date (this needs to be calculated)(desired output)
10/08/2019 10/13/2019
11/01/2019 11/03/2019
11/07/2019 11/10/2019
If Getdate() is the field from your database and Sunday is day of week 1 then this should work.
Select dateadd (dd,(7- datepart(dw,getdate()) + 1),cast(getdate() as date))

Oracle: select max date per month per year from table

I have a table with a set of business dates I need to select the max date per month and year tried using the last_day function but that returns the last day not the max date of that month.please help me out.
MAX is an aggregate function, so you need to figure out how to group all of the days of the month together. The easiest way to do that is apply a function that will return the same value for every day in that month. LAST_DAY would work, but I prefer TRUNC (with 'MM' specified).
SELECT MAX(your_column) FROM your_table GROUP BY TRUNC(your_column, 'MM')

microsoft SQL - returning previous week's data

I have a script that I refresh every week to get the sales data of the last week with the duration starting from last week's Sunday and ending with last week's Saturday. For example if I am running the script in any day within the week from 09/18/16 to 09/24/16, I want to get the sales data spanning from 09.11.16 to 09.17.16.
What script/syntax can I use to get this data if I want to refresh in any day of the current week to get the previous week's data?
Appreciate your time,
Thanks!
You can try the following
declare #date date = getdate()
select dateadd(wk,-1,dateadd(dd, -(datepart(dw, #date)-1), #date)) as [Start],
dateadd(wk,-1,dateadd(dd, 7-(datepart(dw, #date)), #date)) as [End]
Here a working demo
Hope this will help you

Get the date one month before and it's always the last day of the month

In my db, I have a column, 'Transaction Date' with datetime datatype. For instance, '2011-05-31 00:00:00.000'.
I would like to create a SQL Query by selecting data with whereby the 'Transaction Date' column date is one month before the #InputDate.
I have tried with...
DATEADD(MONTH,-1,#InputDate) and it returns '30-May-2011', which is not what i want!
I want the value returns will always be the last day of the month like '31-May-2011'
Use the following scripts:
Last Day of Previous Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) LastDay_PreviousMonth
Last Day of Current Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) LastDay_CurrentMonth
Last Day of Next Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)) LastDay_NextMonth
Last Day of Any Month and Year:
DECLARE #dtDate DATETIME
SET #dtDate = '8/18/2007'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
LastDay_AnyMonth
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
GETDATE() can be replaced by your input date
Last day of same month
SELECT DATEADD(m, DATEDIFF(m, -1, '2011-05-31'), -1)
Last day of last month
SELECT DATEADD(m, DATEDIFF(m, 0, '2011-05-31'), -1)
I think you're asking, given any date, for the last day of the previous month?
If so, the following works (where CURRENT_TIMESTAMP is being used as the date to search from, '20010101' and '20001231' are constants):
select DATEADD(month,DATEDIFF(month,'20010101',CURRENT_TIMESTAMP),'20001231')
It works because the relationship between the two constant dates is that, compared to '20010101', '20001231' was the last date of the month before.
Since SQL Server 2012 you can use the EOMONTH built-in function to get the last day of a month.
So, to get the last day of the previous month you can use this query:
SELECT EOMONTH(GETDATE(), -1)
Where instead of GETDATE() you can put your Date var.
SELECT EOMONTH(#InputDate, -1)

Resources