Forum Discussion
It would be easier to give you query guidance if we knew what we are querying. tell us about your table, its columns... Or at least the query you're wanting to update.
Given what you provided, this might help:
DECLARE @currentDateTime DATETIME = DATEADD(HOUR, -4, GETDATE()) -- Step 1: Adjust current date by subtracting 4 hours
DECLARE @formattedDateTime VARCHAR(20) = CONVERT(VARCHAR, @currentDateTime, 103) -- Step 2: Format date to dd/MM/yyyy
DECLARE @dayOfMonth INT = DAY(@currentDateTime) -- Step 3: Extract day of the month
DECLARE @daysInMonth INT = DAY(EOMONTH(@currentDateTime)) -- Step 4: Get the number of days in the current month
-- Step 5: Calculate the ratio (Day / DaysInMonth)
DECLARE @ratio DECIMAL(5, 4) = CAST(@dayOfMonth AS DECIMAL) / @daysInMonth
SELECT @ratio AS Ratio;
Thanks for your response, Rob.
Here is a snippet of my query and what it returns. There is more to it (needs to return a different query based on parameter selections), but similar queries are being ran regardless of their selections.
I am running a union on the time, so I get Current Period and Prior Year (Same period) so they can compare on a graph.
I do believe adding in another column where the ratio is applied would be the best approach, but it is a matter of getting it into this business rule. I will try to apply what you have provided me with, thank you. Let me know if you have any further suggestions.
- DanielWillis2 months agoValued Contributor
Since you mentioned SQL is not your strong suit, before going further I would strongly suggest you have a look at this article and understand how to pass parameters/variables into your SQL the right way. It is less error prone and less of a security risk: https://community.onestreamsoftware.com/t5/Community-Blog/SELECT-Like-a-Boss-With-Query-Parameters/ba-p/22426
- Daniel
Related Content
- 2 years ago
- 2 years ago