I am using a Stored Procedure to get data from database. I want to get the values based on current date and move 23 months before current date.
In a Stored Procedure I declare current date with
DECLARE @CurrentDate AS DateTime = GetDate();
Data for current month with a formula from I will only mention the date part because is too long and I know the rest is correct
WHERE DATEPART(YEAR, tr.DataInitierii)=DATEPART(YEAR, @CurrentDate) AND DATEPART(MONTH, tr.DataInitierii)=DATEPART(MONTH, @CurrentDate)
and I try to get previous values with
WHERE DATEPART(YEAR, tr.DataInitierii)=DATEPART(YEAR, DATEADD(MONTH,-1,@CurrentDate)) AND DATEPART(MONTH, tr.DataInitierii)=DATEPART(MONTH, DATEADD(MONTH,-1,@CurrentDate))
and so on until
WHERE DATEPART(YEAR, tr.DataInitierii)=DATEPART(YEAR, DATEADD(MONTH,-23,@CurrentDate)) AND DATEPART(MONTH, tr.DataInitierii)=DATEPART(MONTH, DATEADD(MONTH,-23,@CurrentDate))
but the result is NULL despite having data in the database.
What am I doing wrong within the condition?

Marius VasilePosted Mar 9, 2025, 5:43 PM
Ok, now I get full data even 0 if value in database is null but the following code part is not working as intended, final result is null not matter values are retrieved from database. I guess this SET @currentResultTOT = CONVERT(DECIMAL(6,2), (@sumATOT * 200000) / @sumTOT); is not working as intended.
Marius VasilePosted Mar 9, 2025, 2:08 PM
I rewrite the query, simplified and ran in SSMS. I got some data but not the final SUM. Can anyone tell me please what am I doing wrong?
result from SSMS
Marius VasilePosted Mar 9, 2025, 8:52 AM
Full Stored Procedure below
in code behing I get the data with
Marius VasilePosted Mar 9, 2025, 8:41 AM
In tblOmOra Year is int and Month is string, that's why I was trying to use
I tested this and is working in SSMS
Jignesh KumarPosted Mar 9, 2025, 8:37 AM
Hello Marius,
You can use this and give a try,
Note : Have you cheked, How your month field is stored in tblOmOra
Check this part in SET "AND Month=CONVERT(nvarchar,DATENAME(MONTH,@CurrentDate))"
Marius VasilePosted Mar 9, 2025, 8:20 AM
Thank you Jignesh for your answer. What am I trying to do is put in a Chart monthly values for current dateuntil 23 months before. I did this for one year period with the same approach I mentioned here but now I am facing difficulties. See resolve here https://www.csharp.com/forums/looping-through-int-values-in-a-stored-procedure.
Jignesh KumarPosted Mar 9, 2025, 8:06 AM
Hello marius,
You want to get all 23 years data combined, you can use this query,
You want to get month wise data,
Marius VasilePosted Mar 9, 2025, 5:58 AM
I found the faulty part after I checked bit by bit in SSMS. It seems that
returns NULL. But when I run the query in SSMS
I get the correct value. Can anyone tell me what is wrong in the SET query? @cne0 isw declared as int, value from database is int
Marius VasilePosted Mar 8, 2025, 2:56 PM
Thank you Sophia, I have to compare by year and date, see example of a full code below
I have an aggregate function for each month and because the range may even strech in three different years I have to compare based on year and month
Sophia CarterPosted Mar 8, 2025, 2:49 PM
It seems like you are on the right track with your SQL queries to retrieve data for the current month and the previous months. However, the issue you are encountering with getting NULL results despite having data in the database might be due to the way you are comparing dates.
When dealing with date parts in SQL Server, it can be more reliable to compare the full date values rather than individual parts like year and month. Here are a few considerations and suggestions to tackle this issue:
1. Full Date Comparison: Instead of comparing only the year and month parts of the date, you can try matching the full date to ensure accuracy. For example:
2. Debugging Query: To investigate further, you can try running queries to check for data specifically from the previous months to see if the issue lies within the comparison conditions.
3. Data Type Compatibility: Ensure that the data type of the columns (like `tr.DataInitierii`) and variables (like `@CurrentDate`) you are comparing are compatible. If there is a mismatch, it can lead to unexpected results.
4. Handling Time Component: If `tr.DataInitierii` includes a time component along with the date, you might want to compare the date part only to avoid precision issues.
5. Testing with Simplified Queries: To isolate the problem, you can simplify your queries and conditions to focus on retrieving data for a specific month (e.g., the previous month). This can help in identifying any discrepancies.
By ensuring that your date conditions match correctly, you should be able to retrieve the desired data spanning across the last 24 months effectively. If you encounter further challenges or need more specific assistance with your SQL queries, feel free to provide additional details for a more targeted solution.