I want to calculate weeks and days between 2 Dates, Like that
IN SQL QUERY
if Date from : 10-01-2018 Date to : 19-01-2018
so i want the result "1 week and 2 days"
If you are using MSSQL (Transact-SQL) you can use the datediff-method:
DATEDIFF ( datepart , startdate , enddate )
whic in your case would be
DATEDIFF(day,10-01-2018, 19-01-2018);
And then divide the days with 7 and take the remainer as days.
More info at MS Docs
Try this.
DECLARE #From datetime
DECLARE #To datetime
SET #From ='20180110'
SET #To ='20180119'
SELECT DATEDIFF(ww,#From, #To) AS Week,
DATEDIFF(dd,#From, #To)%7 AS Days
Use the DATEDIFF function with datepart=day.
The number of weeks is the result divided by 7 and the number of days is the remainder
Related
I am trying to write a SQL query that shows the count of the days and date depending upon the financial period it falls in. The financial period starts 5 days before the month end eg march 27th to 26th April
For the above mentioned period if the day is 29th march, the count of the day should be 3 and the date should be 2020-04. The date should adjust depending upon the period it falls in.
I tried to adress the second part of this query by writing the below script but it does not bring any result
declare #date datetime
set #date = getdate()
SELECT format (date,'yyyy-MM-dd') as date
where #date
between dateadd(day,-5,EOMONTH(getdate(),-1)) and dateadd(day,-5,EOMONTH(getdate()))
updated to include 5 days from previous month.
Is this what you are looking for the second part?
Edit: Modifying the query. I guess this should give you what you need for both the parts.
you can try changing the dates in set #date.
Please note that the -4 instead of -5 is done intentionally as you said the financial month starts 5 days earlier. For March, 31 - 5 would give 26, but it should start on 27 right? so that on 29th the no. of days should be 3 including 27 and 29. Anyways, the query should be self explanatory, might just need to change the number depending on your requirement.
declare #date datetime
--set #date = getdate()
set #date = '2020-02-14'
SELECT format(#date,'yyyy-MM-dd') as date,
case WHEN #date < dateadd(day,-4,EOMONTH(#date)) THEN format(EOMONTH(#date),'yyyy-MM')
ELSE format(EOMONTH(#date, 1),'yyyy-MM') END
AS FinancialMonth,
CASE WHEN #date < dateadd(day,-4,EOMONTH(#date)) THEN DATEDIFF(day,dateadd(day,-5,EOMONTH(#date,-1)), #date)
ELSE DATEDIFF(day, dateadd(day,-5,EOMONTH(#date)), #date) END
AS CountDays
The following code will calculate what you need. Please read the comments in the code itself.
-- First, lets declare all the variables we need.
-- I tried to name them so they are selfexplanatory.
declare #inputdate DateTime,
#financialPeriodStartDate DateTime,
#EndOfTheMonth DateTime,
#nextFinantialMonthYear DateTime,
#previousFinancialPeriodStartDate DateTime
-- Now we calculate some of the values
set #inputdate = GETDATE() -- Let's use Today's date, but you can change this to any date.
set #EndOfTheMonth = EOMONTH(#inputdate) -- This is the end of the month for the input date
set #financialPeriodStartDate = DATEADD(DAY,-5,EOMONTH(#inputdate)) -- This is you Financial Period Start Date for the giving input date.
set #previousFinancialPeriodStartDate = DATEADD(DAY,-5,EOMONTH(DATEADD(MONTH, -1, #inputdate))) -- Also calculates the Start Date of the precious financial period.
set #nextFinantialMonthYear = DATEADD(MONTH, 1,#financialPeriodStartDate) -- This is the start date of the next financial period.
-- In the following Select, it calculates the values you need, based in the previous variables.
select
#inputdate as Today,
#EndOfTheMonth as EndOfMonth,
#previousFinancialPeriodStartDate as PreviousFinancialPeriodStartDate,
#financialPeriodStartDate as FinacialPeriodStartDate,
CASE WHEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 ) < 0
THEN DATEDIFF(DAY , #previousFinancialPeriodStartDate, #inputdate + 1 )
WHEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 ) >=0
THEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 )
END as DaysFromFPStartDate,
STR(YEAR(#nextFinantialMonthYear)) +'-'+LTRIM(RTRIM(STR(MONTH(#nextFinantialMonthYear)))) as NextFinantialPeriodMonthAndYear
I hope it helps.
i have two date like this
-4 year and 6 month
-2 year and 3 month
i want to calculate difference between this two date ?
If you have a datetime value then you can use Datediff function to calculate the difference between two datetime values in SQL Server.
SELECT DATEDIFF(day, '2004-06-01', '2008-06-01') AS "Number of Days";
I used below SQL to get last day of the month, but I need last seven days of the month:
DECLARE #dt as DATETIME = '7/29/2018'
DECLARE #LastDayOfTheMonth as DATETIME = DATEADD(DAY, -1, DATEADD(Month, 1, DATEADD(DAY,1 - DAY(#dt),#dt)))
SELECT #LastDayOfTheMonth
I could also use this SQL function in order to get above result, but I need last 7 days of any month's records.
SELECT EOMONTH('7/29/2018')
Just use DATEADD again to take 1 to 6 more days off #LastDayOfTheMonth.
SELECT #LastDayOfTheMonth, DATEADD(DAY,-1,#LastDayOfTheMonth), ..
If you want to return 7 values from a function, suggest making it a table function.
I need a simple sql script where i can grab the rows with a date between sunday 2 weeks ago and saturday the previous week.
I need the query to return the elements no matter what day of this week I run the query.
Lets say I run the query today: Thursday 12. dec 2016. (12-08-2016)
I need to get this interval:
SELECT * FROM table WHERE date BETWEEN '11-27-2016' AND '12-03-2016'
DECLARE #StartInterval DATE,
#EndInterval DATE,
#Today = GETDATE()
SET #EndInterval = DATEADD(dd,-1,DATEADD(dd,-1*(DATEPART(dw,#Today)-1),#Today))
SET #StartInterval = DATEADD(dd,-6,#EndInterval)
SELECT *
FROM table
WHERE date BETWEEN #StartInterval AND #EndInterval
You can use:
select * from table where date between next_day(date, 'SUN')-21 AND
next_day(date, 'SAT')-14
I have a SQL query that prompts for a startdate and enddate, Between #startdate And #enddate. But what I am trying to do is have the same query automatically look 3 months forward from today's month using a dateadd / getdate code, but can not figure out how to integrate the two?
In sql-server, which I assume because you are referencing GETDATE() and DATEADD()
DATEADD(MONTH,3,GETDATE())
IN Oracle based on Why is the GETDATE() an invalid identifier, and Equivalent function for DATEADD() in Oracle
ADD_MONTHS(SYSDATE,3)
Based on your comment and if Siebel is on sql-server you would need
Where vwS_CoPrimPolicy.S_PrimPolicyExpDT Between #startdate And #enddate And vwS_Co.S_CoStatus = 'Customer' And vwS_Co.S_CoType = 'Automotive
to become
where vwS_CoPrimPolicy.S_PrimPolicyExpDT Between
DATEFROMPARTS(YEAR(DATEADD(MONTH,3,GETDATE())),MONTH(DATEADD(MONTH,3,GETDATE())),1)
AND DATEADD(DAY,-1,DATEFROMPARTS(YEAR(DATEADD(MONTH,4,GETDATE())),MONTH(DATEADD(MONTH,4,GETDATE())),1))
And vwS_Co.S_CoStatus = 'Customer' And vwS_Co.S_CoType = 'Automotive
To Break that down a little because you are actually looking for the start of the month 3 months from today to the end of the month 3 months from today and those months could be 28,29,30,or 31 days long you actually need to find the date 3 months ahead then get the beginning of the month DATEFROMPARTS() is useful for that. Then for the end of the month, find the date 4 months out and go back a day.