not getting previous month record using datepart in sybase - sql

Using below depart syntax to fetch for previous month record, it is working fine till previous year,however it is giving 0 value in January month.How can we get pervious month with date part even if year is change ?
DATEPART(month(GETDATE()) -1

I understand that I used another type of DB, but I want to give a hint. I am using sql server 2019.
Firstly, you need to substitute date and only then take datepart from it.
Queries:
--dateadd -1 would subtract 1 from current month
--(Jan - 1 2022), would be December 2021
select datepart(month, dateadd(month, -1, getdate()))
--also date add covers internally the problem with 30,31 days.
--May always has 31 days, April 30. So -1 subtraction from 31th of May,would result in 30th of April.
select dateadd(month, -1, cast('2021-05-31 10:00:00' as datetime))

Related

Custom month numbers that take last 30 days instead of Number of month (SQL Server)

I am trying to create a lag function to return current month and last month streams for an artist.
Instead of returning streams for Feb vs Jan, I wan the function to use the last 30 days as a period for current month, and the previous 30 days as the previous month.
The query that I am currently using is this:
SELECT
DATEPART(month, date) AS month,
artist,
SUM([Streams]) AS streams,
LAG(SUM([Streams])) OVER (PARTITION BY artist ORDER BY DATEPART(month, date)) AS previous_month_streams
FROM combined_artist
WHERE date > DATEADD(m, -2, DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()-7), CAST(GETDATE()-7 AS DATE)))
GROUP BY DATEPART(month, date), artist;
While this works, it is not giving me the data I need. This is returning the sum of streams for February vs the Streams for the month of January. February seems very low because we only have one week worth of data in February.
My goal is to get the last 30 days from the max date in the table using a lag function. So if the max date is Feb. 7 2023, I want the current month to include data from Jan. 7 2023 - Feb. 7 2023, and the previous month to include data from Dec. 7 2022 - Jan. 7 2023. I am thinking to create a custom month date part that will start from the max date and give a month number to the last 30 days . (2 for Jan 7 - Feb 7, 1 for Dec 7 - Jan-7...) I am not sure how to go about this. This is in SQL Server and I am looking to use the lag function for performance reasons.
I think you could probably use something like datediff(d, date_you_care_about, max_date)/30 in your group by and partition by clauses.
The basic idea is that integer division rounds down, so if the difference between the dates is < 30, dividing it by 30 is 0. If the difference is >=30 but less than 60, dividing it by 30 is 1. And so forth.
You can see a proof of concept in this Fiddle.

Need Last Month of Last Year Data

I am trying to write a query that looks at the last month and last month of last year data to compare. So since I am writing this in August of 2022 I want to see July of 2022 and July of 2021. I know I can run a between dates and have to update it every month but I want this to feed into PowerBi and be an automated report.
I can get last month's just fine the issue is last year last month. I have tried a couple of different queries and they either error out and fail or return nothing.
I am currently skipping the part for last month as I know it works. Can I please get some help with the last year part?
Declare #StartofCurrentMonth datetime
Set #StartofCurrentMonth = dateadd(month, DATEDIFF(MONTH,0,CURRENT_TIMESTAMP),0)
Select BranchID, ItemID, CAST(Ledgerdate AS Date) As 'LedgerDate', TransactionType
From ItemLedger
Where --LedgerDate >= DateAdd(Month, -1, #StartofCurrentMonth) AND LedgerDate < #StartofCurrentMonth OR
**Ledgerdate >= DATEADD(Year, -1, #startofcurrentMonth) AND LedgerDate <** **DATEADD(Month,-12,#startofCurrentMonth)**
AND TransactionType IN ('Item.Move', 'Item.Putaway')
Order By LedgerDate
Thank you
I figured it out finally.
I changed the last year stuff to:
Ledgerdate >= DATEADD(MONTH, -13, #startofcurrentMonth) AND LedgerDate < DATEADD(Month,-12,#startofCurrentMonth))
Thank you.

SQL SELECT SUM BETWEEN 1 Year from now to start ot the year

so I'm kinda stuck with a little query to get the SUM of the time period I want. So it already works but I want that it's automatically between 1 year from now and the beginning of the year.
SELECT SUM(N_EXCL), S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
from sao.INVOICE_P i
WHERE D_INVOICEDATE > DATEADD (year, -1, getdate ())
AND D_INVOICEDATE < DATEADD (month , -6 ,Getdate () )
GROUP BY S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
Currently I would have to change the query every month but I try to find a way to avoid it.
Ok. Maybe i wasn't clear enough. My problem is the -6 i have to write down. I want to switch it to a parameter that will automatically change up the date so that i get data from 01.01.2021 - 22.06.2021. So same date, but different year.
If I understand your question, you should be able to do this:
SELECT SUM(N_EXCL), S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
from sao.INVOICE_P i
WHERE D_INVOICEDATE between DATEADD(year, -1, getdate()) AND DATEFROMPARTS(getdate(), 1, 1)
GROUP BY S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
So instead of using where and I'm using where between. And I'm checking for todays date last year till 1st of January current year.
If you need from 1st of January previous year you can use:
between DATEFROMPARTS(year(getdate() -1), 1, 1) AND DATEADD(year, -1, getdate())

Get Week Before Last

Hi I am trying to pull data which will excluded the previous 2 weeks and include the 2 weeks beofre that.
so for example if today is the 31st of the month i want to exclude all data between the 15th and 31st and want to see only the 1st to the 15th
AND E.EventCreatedDateD between DATEADD(day,-14,GETDATE()) AND DATEADD(day,-31,GETDATE())
You are getting 0 values from your statement because the dates are backwards. Put the earlier date first:
AND E.EventCreatedDateD between DATEADD(day, -31, GETDATE()) AND DATEADD(day, -14, GETDATE())
The order of the expressions for the between is important. The lower value always needs to be first and the higher value second.

SQL - get last month data

I have been using this query to extract information from last month
SELECT *
FROM Member
WHERE DATEPART(m, date_created) = DATEPART(m, DATEADD(m, -1, getdate()))
with the end of the year approaching, will this automatically pull Dec 2012 when i run it in Jan 2013 ?
Yes. your getdate() function will give the current date when the query is run. And you are adding -1 to the month and comparing month of date_created column and the last month. But I think you should also do comparison of year. You should add two conditions month and year both.
Yes, it will pull December data. But it will pull December data from any year, not just 2012
Yes, it will. DATEADD is a SQL internal function that adds to the full date, not just the selected part (day, month, year).