What we pass 0 In date functions As Arguments for calculation of Start Date,End Date what is the explanation for that argument? - sql

here like these queries,
I.e: here is First date of year is calculated ,
select DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
and here 0 are used,what is the explanation for that?

You mean "Why", obviously.
0 is an arbitrary date which is easy to write, I think 1st January 1753. It does not matter in the all-too-common dateadd(datediff()) formula, ANY date/time would be just as good if put in both the dateadd's and the datediff's arguments. But, 0 is easy to write, plus everyone is doing the same and it creates some comfort seeing it and understanding it.
The way dateadd-datediff works is this: eg in your query
DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
, you want the first moment of the year of getdate(). How do you do this? You ask how many years have passed, from 1st January 1753, to getdate(3 July 2018), which results in 265. You then add that number(265) of years to 1st January 1753. Because you only added the years, and not months, days, hours etc you will get 1/1/2018 00:00:00.

Related

sql Teradata difference in months

I need to get the difference between dates, but I just need to get the whole months that have passed. So for example between "1990-05-24" and "1990-05-27" it should say 0. It would also be 0 for "1990-05-02" and "1990-05-29" because the month has not finished.
I already got the difference in months using MONTHS_BETWEEN(), but I get months with decimals, and ROUNDing is not an option since sometimes it should be up and sometimes down.
I thought about setting al dates to day 01. In both colums Closing_date and Opening_date. But can't figure out how to do it.
I think you want to count boundaries between months. If so, you can use months_between() after truncating to the first of the month:
months_between(trunc(date1, 'MON'), trunc(date2, 'MON')

SQL compare Workdays

Currently I have 2 years worth of Sales data, and my company wants a weekday to weekday comparison between the 2 years
basically the Sales Numbers for Wednesday April 1st 2020 should be compared to Wednesday April 3rd 2019 (weekday to weekday comparison, instead of calendar year)
i'm trying to come up with some sort of case statement that will allow me to add a helper column so I can then put it into an excel Pivot table, or use a join, but I'm a bit stumped on how to go about it
Basically I thought if I could come up with some way to assign a sequence to each workday, it could be done
for example Monday #1 in April could get the code MApril1 Monday #2 in April could get MApril2 Monday #1 in June could get MJune1 and so forth (I could just do that for both years and that would make it very easy)
but I just cant think of a way to write that sequence or case, any suggestions?
long story short lets say I gave you the date Wednesday April 8th 2020, how would you identify this as the second Wednesday of April 2020?
Not sure if I understood clearly but I will try to help.
Don't hesitate to tell me if I'm wrong.
I assumed you used SQL Server. Please add the SQL you use into the tags.
I would count the week number.
set datefirst 1; -- depending on the language
declare #weekNb int;
select #weekNb = datepart(week, #yourInitialDate); -- getting the number of the week
declare #dayNb = int;
select #dayNb = datepart(weekday, #yourInitialDate); -- getting the number of the day
And now, using the function from this post I would get the day from the current year :
select dbo.date_from_week_number_day(YEAR(GetDate(),weekNb,dayNb);

How can I Format the Date so that the fiscal week starts in December?

I want to format a date as follows: Y17W15, but there is no option to set the start of the year. However, there is no consistent way of calculating this. I cannot just subtract a month (other times I will need to show the month too), and I cannot just add 4 or 5 to the week field due to leap years, etc.
Our year starts on a Saturday that is closest to December 1. This means if November 30 is on a Saturday, the Fiscal Year will start on November 30.
Currently what I have is below, which works fine except it shows Y17W10. The easiest option in my head is to have a way to actually set the start of a fiscal year, but if I have to go through a bunch of if statements it's okay as long as it works.
MsgBox(Format(Now, """Y""yy""W""mm"""))
Thanks for your time!
I am aware that: Given a 4-5-4 calendar and a date, how do I determine what fiscal week that date falls in? exists but I am looking for an answer that isn't as hard-coded.
Michael

Get the EOM and MOM of any month in SQL Server 2005

I've periods of time every 15 days, MOM and EOM.
What I need to check is if a date value on a date field is prior to the current period minus 1.
For example, if today is 12/29, the period is 12/31, and i need to check
if prior < 12/15
How can i get the EOM (End Of Month, i mean, the last day of the month) and the MOM (Middle of month, it's like every 15th of month) with GETDATE() function without doing a DATEADD with -15 days (because in feb will be fail, and i don't care the month)
Any help or work around will be preciated.
Thanks
If you need the value 15 then put it in your code.
If that is against your company's policies then challenge the person that made that policy. Writing 5 lines of code to replace two characters is not a good coding...
If writing the 5 lines made your application much more flexible then maybe I could understand, but you are still "hard coding" 15 into your comparisons.
Thinking in a work around, what I did it was this:
If actual day < 15 then get the month actual, convert to the first day of the month (01) and minus 1 day. I get the last day of the prior month. (EOM - 1 period)
If actual day > 15, then the prior period (MOM - 1 period) is: 15 of actual month.
It's a query with if structure.
If someone has a better answer, please answer it and I'll be accept it.
Thanks :)

Battling Datediff in SQL

I am writing a little query in SQL and am butting heads with an issue that it seems like someone must have run into before. I am trying to find the number of months between two dates. I am using an expression like ...
DATEDIFF(m,{firstdate},{seconddate})
However I notice that this function is tallying the times the date crosses the monthly threshold. In example...
DATEDIFF(m,3/31/2011,4/1/2011) will yield 1
DATEDIFF(m,4/1/2011,4/30/2011) will yield 0
DATEDIFF(m,3/1/2011,4/30/2011) will yield 1
Does anyone know how to find the months between two dates more-so based upon time passed then times passed the monthly threshold?
If you want to find some notional number of months, why not find the difference in days, then divide by 30 (cast to FLOAT as required). Or 30.5-ish perhaps - depends on how you want to handle the variable month length throughout the year. But perhaps that's not a factor in your particular case.
The following statements have the same startdate and the same endate. Those dates are adjacent and differ in time by .0000001 second. The difference between the startdate and endate in each statement crosses one calendar or time boundary of its datepart. Each statement returns 1. ...
SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000'); ....
(from DATEDIFF, section datepart Boundaries ). If you are not satisfied by it, you probably need to use days as unit as proposed by martin clayton
DATEDIFF(m,{firstdate},ISNULL({seconddate},GETDATE())) - CASE
WHEN DATEPART(d,{firstdate}) >= DATEPART(d,ISNULL({seconddate},GETDATE()))
THEN 1
ELSE 0
DATEDIFF is like this by design. When evaluating a particular time measurement (like months, or days, etc.), it considers only that measurement and higher values -- ignoring smaller ones. You'll run into this behavior with any time measurement. For example, if you used DATEDIFF to calculate days, and had one date a few seconds before midnight, and another date a few seconds after midnight, you'd get a "1" day difference, even though the two dates were only a few seconds apart.
DATEDIFF is meant to give a rough answer to questions, like this:
Question: how many years old are you?
Answer: some integer. You don't say "I'm 59 years, 4 months, 17 days, 5 hours, 35 minutes and 27 seconds old". You just say "I'm 59 years old". That's DATEDIFF's approach too.
If you want an answer that's tailored to some contextual meaning (like your son who says "I'm not 8! I'm 8 and 3-quarters!, or I'm almost 9!), then you should look at the next-smallest measurement and approximate with it. So if it's months you're after, then do a DATEDIFF on days or hours instead, and try to approximate months however it seems most relevant to your situation (maybe you want answers like 1-1/2 months, or 1.2 months, etc.) using CASE / IF-THEN kinds of logic.