Here I want to check whether date from database lies between financial year or not.
I am using following query to check date but it is working properly for year only. if i want to check according to month and year then i got wrong result.
Here is my query:
SELECT *
FROM Payments INNER JOIN Subsciber ON Subsciber.SubId = Payments.SubId
WHERE DATEPART(YEAR, Payments.SaveOn) BETWEEN 2010 AND 2011
AND DATEPART(MONTH, Payments.SaveOn) BETWEEN 4 AND 3
payments.saveon >= CONVERT(DATETIME, '20100401', 112) AND
payments.saveon < CONVERT(DATETIME, '20110401', 112)
Related
I have a query that currently looks at receipts back 28 days from the date of purchase.
AND PAYMENT_DATE >= DATEADD(DD, -28, CONVERT(DATE, GETDATE()))
Now I this should go back to November 15 of the previous year, still using PAYMENT_DATE.
Is it possible to get help on how to accomplish this change?
Thank you.
Just subtract a year from todays date and build a new date using DATEFROMPARTS
AND PAYMENT_DATE >= DATEFROMPARTS(YEAR(GETDATE())-1, 11, 15);
Use the follwing code
datediff(dd, PAYMENT_DATE, '20201115') = 0
Another approach
AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, PAYMENT_DATE))) = '15/11/2020'
I have a table of names and associated birthdates. I can retrieve the names of everyone whose birthday is today by matching the MONTH and DAY dateparts between the birthdate in the database and the current date. However, I need to pull "looking forward" lists of, say, all birthdays in the next two weeks.
The obvious solution would be a computed column for each person, showing his/her "birthday this year". It's easy to pull the month and day from the birthday, add the current year, and cast the whole string as a date. That way I could just retrieve those whose "birthday this year" is within X days of the current date. However, I have one person with a birthday on February 29, and there isn't a Feb. 29 every year, so the calculated column "chokes" when I query or open the table with the following error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Suggestions on a way to either make a computed column work properly in this situation, or an alternative way to query using the date of birth in the table?
Change your query from using convert to using try_convert which will not error on nonexistent date but will instead return NULL. This will exclude your Feb 29 birthday client if running year is not leap.
Your approach won't work for the end of December, because the year changes. Here is a different approach:
Add the number of years to the date and let the database handle leap years.
Then do the comparison.
So, the logic for the first is:
select dateadd(year, year(getdate()) - year(dob), dob)
Then to get dates of birth in the next two weeks:
where dateadd(year, year(getdate()) - year(dob), dob) >= convert(date, getdate()) and
dateadd(year, year(getdate()) - year(dob), dob) < dateadd(14, day, convert(date, getdate())
However, this still doesn't handle dates of birth that could be next year. So, to handle the end of year, consider that as well:
where (dateadd(year, year(getdate()) - year(dob), dob) >= convert(date, getdate()) and
dateadd(year, year(getdate()) - year(dob), dob) < dateadd(14, day, convert(date, getdate())
) or
(dateadd(year, 1 + year(getdate()) - year(dob), dob) >= convert(date, getdate()) and
dateadd(year, 1 + year(getdate()) - year(dob), dob) < dateadd(14, day, convert(date, getdate())
)
To do this - you need to calculate the current year's DOB and the persons next date of birth. To calculate the current year date of birth we can use a simple calculation:
dateadd(year, datediff(year, DateOfBirth, CurrentDate), DateOfBirth)
Then - we need to calculate the next date of birth, which is simply adding a year if the current year DOB is less than CurrentDate:
dateadd(year, iif(CurrentDOB < CurrentDate, 1, 0), CurrentDOB)
Now - it is just a matter of checking if the next DOB is in the range. Here is some sample data to show how to put this together.
--==== Some sample dates of birth - including leap year DOB's
Declare #testData Table (DateOfBirth date);
Insert Into #testData (DateOfBirth)
Values ('1992-01-09'), ('2020-02-29'), ('1965-09-30'), ('1984-02-29');
--==== Test using different dates
Declare #current_date date = '2021-02-28';
--==== Use CROSS APPLY to calculate current year DOB and Next DOB
Select *
From #testData As td
Cross Apply (Values (dateadd(year, datediff(year, td.DateOfBirth, #current_date), td.DateOfBirth))) As y(CurrentDOB)
Cross Apply (Values (dateadd(year, iif(y.CurrentDOB < #current_date, 1, 0), y.CurrentDOB))) As n(NextDOB)
Where n.NextDOB <= dateadd(day, 14, #current_date);
If today is 2021-02-28 then the birthdays that fall on the 29th will be included. If today is 2021-03-01 then those will not be included because they would be calculated as 2022-02-28 which is not within 14 days.
I want the exact date as 22/06/2015 from the query
whose Joining date should be exact
22/06/2015
which is exact 6 months back from todays date
I tried like below
Select date_of_joining,* from emp_mst Where Dt_Of_Join >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, getdate())), 0)
but it didn't worked.
what is the exact query for that ?
I am using SQL- server- 2005
if you want the EXACT joining date ( /date_of_joining.... /Dt_Of_Join)
what about
select distinct employee.name from emp_mst where date_of_joining = DATEADD(month, -6, GETDATE())
or if you want the actual date returned in a different format:
CONVERT(Date,DATEADD(month, -6, GETDATE()), 103)
which is applicable if you select this field
I'm not a SQL Server guru, but I found easy answers everywhere for this.
Try this link to another post which explains this exact question SQL Server 2005: how to subtract 6 month
You refer to the word "exact" date, so you don't need the datediff section, you can just subtract 6 months from the current date using "dateadd" which will give you a precise date. Just remember to correctly type cast else you will have to be accurate to the millisecond.
SELECT employee_name
FROM emp_mst
WHERE Dt_Of_Join = Cast(DATEADD(month, -6, GETDATE()) As Date)
ORDER BY Dt_Of_Join DESC
I think he just need to know how to remove time from date
Note that you need to handle time part effectively
Select * from emp_mst
Where
Dt_Of_Join >= dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),0)
and
Dt_Of_Join < dateadd(month,-6,dateadd(day,datediff(day,0,getdate()),1)
You want previous 6 month date so use dateadd() and for your date formate DD/MM/YYYY you should try to convert(varchar(10),date,101).
Select date_of_joining,* from emp_mst Where
Dt_Of_Join=convert(varchar(10),dateadd(month,-6,getdate()),101)
I am trying to group the number of hours that employees worked for the last 4 weeks but I want to group them on a weekly basis. For example:
WEEK HOURS
Feb 24 to March 2 55
March 3 to March 9 40
March 10 to March 16 48
March 17 to March 23 37
This is what I have so far, please help. thanks
SET DATEFIRST 1
SELECT CAST(MIN( [DT]) AS VARCHAR(20))+' TO '+CAST (MAX([DT]) AS VARCHAR(20)) AS DATE,
SUM(HOURS) AS NUM_HRS
FROM MyTable
GROUP BY DATEPART(WEEK,[DT])
HAVING COUNT(DISTINCT[DT])=7
Create a Calendar auxilliary table, with Year, Month, Week, Date columns (you can also add holidays and other interesting stuff to it, it has many potential uses) and populate it for the period of interest.
After that, it's as easy as this:
SELECT sum(hours), cast(min(date) as varchar), cast(max(date) as varchar)
FROM Calendar c
LEFT OUTER JOIN MyTable h on h.Date = c.date
GROUP BY year, week
ORDER BY year, week
SET DATEFIRST 1
SELECT DATEPART(WEEK,DT) AS WEEK,
SUM(HOURS) AS NUM_HRS
FROM MyTable
WHERE DT >= DATEADD(WEEK, -4, GetDate()),
GROUP BY DATEPART(WEEK,[DT])
Try something like
SELECT
DATEADD(DD,
CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7,
'1/1/1900') [WeekBeginDate],
DATEADD(DD,
(CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7) + 6,
'1/1/1900') [WeekEndDate],
SUM(HOURS) AS NUM_HRS
FROM MyTable t
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)
Though this is the brute force trick, I think in your case it will work.
EDIT : Modified the query a little bit, the error was caused because of the order in which DATEDIFF calculates the difference.
Also here is a SQL FIDDLE with a working example.
EDIT 2 : Updated the Fiddle with the Date Format. To customize the date format, this article would help.
Any help would be great since i'm not good with SQL query :)
Thank you
I have a table called Registration
i would like to get all records of who sign up total from each month.
for example this month is Jun
so the data would bring back
January 500
February 200
March 600
April 100
May 800
Jun 400
what i have now
SELECT count(r.regID) AS totalCount
FROM Registration r with(nolock)
WHERE DATEPART(MONTH, createStamp) = DATEPART(MONTH, DATEADD(MONTH, -1, getdate()))
AND DATEPART(YEAR, createStamp) = DATEPART(YEAR, DATEADD(MONTH, -1, getdate()))
right now its onlu pulling the last month since i dont have any data for Jun
CreatStamp is smalldatetime
It seems like all you would need is:
SELECT YEAR(CREATESTAMP), MONTH(CREATESTAMP), COUNT(R.REGID) AS TOTALCOUNT
FROM REGISTRATION R
GROUP BY YEAR(CREATESTAMP), MONTH(CREATESTAMP)
ORDER BY YEAR(CREATESTAMP), MONTH(CREATESTAMP)
What are you trying to accomplish with the DATEPART...GETDATE() business?