I am trying to update certain fields for employees whose date of joining falls in between 10 Jun and 31 Dec, irrespective of the year. I am trying using 'Between' Operator but it requires year to be included in the dates. Is there a way to generalise it in order to consider Day and Month excluding the Year?
Use the DatePart function - replace thedate with your column, and thetable with the column.
Something like this:
select datepart(MONTH, thedate), datepart(DAY, thedate),*
from thetable
where datepart(MONTH, thedate) between 6 and 12
and datepart(DAY, thedate) between 10 and 31
You may try this:
WITH Emp AS (
SELECT *, DATEPART(MONTH, JoinDate) AS MonthJoin, DATEPART(DAY, JoinDate) AS DayJoin
FROM Employees)
SELECT *
FROM Emp
WHERE (MonthJoin > 1 AND MonthJoin < 12)
OR (MonthJoin = 1 AND DayJoin >= 10)
OR (MonthJoin = 12 AND DayJoin <= 31)
Where Employees is your table and JoinDate is your date of joining in this table
Related
I have a table with birthdates and I want to select all the birthdays that will come in the next 30 days.
The situation is, that all the birthdays are written in the form off 1999-09-15 which means that even if I tried selecting the next 30 days, the birthdays wouldn't show up because the year is 1999.
I am running Microsoft Server 2016.
SELECT * from dbo.EMPLOYEES
WHERE DATE <= DATEADD(day, +30,GETDATE())
and DATE >= getdate()
order by "DATE"
To get the birthdate, we need to work only on the days and the months, not on the year. Thats why we cannot get Where date between 2 dates.
SELECT
dateofbirth_c AS BIRTHDAY
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25) AS AGE_NOW
,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25) AS AGE_30_Days_FROM_NOW
FROM
Employees EMP
WHERE 1 = (FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25))
-
(FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25))
Try the following; check the month and day because year will not match with birthday year that's why you are not getting any data.
SELECT *
from dbo.EMPLOYEES
WHERE month(DATE)>= month(GETDATE())
and day(DATE) >= day (getdate()) and day(DATE) < = day( getdate()) + 30
order by "DATE"
I'm note sure about microsoft server but on postgres just generate list of days and compare like this.
SELECT * FROM table WHERE to_char(user_dob, 'MM-DD') IN
(SELECT to_char(date, 'MM-DD')
FROM generate_series(current_date, current_date + 30, '1 day') AS date);
And now all you have to change that 30 to any days to make it work.
try this
SELECT *
FROM dbo.EMPLOYEES
WHERE DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) >= GETDATE()
AND DATEFROMPARTS(YEAR(GETDATE()) , MONTH(Date), DAY(Date)) <= DATEADD(day, +30, GETDATE())
ORDER BY Date
If you want an accurate result that works for leap years and so on, then:
SELECT e.*
FROM dbo.EMPLOYEES e CROSS APPLY
(VALUES (DATEFROMPARTS(YEAR(GETDATE()),
MONTH(e.date),
DAY(e.date)
)
)
) v(this_year_date)
WHERE DATEDIFF(day, GETDATE(), this_year_date) BETWEEN 0 AND 29 OR
DATEDIFF(day, GETDATE(), DATEADD(year, 1, this_year_date)) BETWEEN 0 AND 29
order by "DATE"
I have a tenure table where I have columns link id, name, join date of an employee.
I am trying to get only those employee whose tenure(work experience) is 2 to 5 years from join date to today's date.
I used query
SELECT ID FROM tenure WHERE DATEADD(year, 2, JoinDate) >= '2018-08-06' AND DATEADD(year, 5, JoinDate) <= '2018-08-06';
My tabular structure is
How can I get only those employees whose working period or tenure is between 2 to 5 years from their join date to today's date?
You can use TimeStampDiff() :
SELECT ID
FROM tenure t
WHERE timestampdiff(YEAR, JoinDate, now()) >= 2 AND
timestampdiff(YEAR, JoinDate, now()) <= 5;
use TIMESTAMPDIFF function
SELECT ID FROM tenure WHERE
TIMESTAMPDIFF(YEAR, date(joindate), date(now()))>= 2 and
TIMESTAMPDIFF(YEAR, date(joindate), date(now()))<=5
To me, your syntax suggests SQL Server. The logic would be:
where joindate >= dateadd(year, -5, getdate()) and
joindate < dateadd(year, -2, getdate())
Compare the Joindate to the expected range based on today's date
select *
from tenure
where joindate between DATE_ADD(cur_date(), INTERVAL -5 YEAR)
and DATE_ADD(cur_date(), INTERVAL -2 YEAR)
I need a SQL select statement to retrieve all employees that their enddate contract will end three month from now and only three month not more or less
It depends what you mean by 3 months from now. If you mean on that exact date, then:
WHERE my_date_column = cast(dateadd(month, 3, getdate()) as date)
Note the cast to date to remove the time component. This is ambiguous and under some circumstances might miss employees or count them twice.
If you want employees whose contract ends in the 3rd month from today, then use:
WHERE DATEDIFF(month, getdate(), my_date_column) = 3
So, if this is January, this will return employees whose contract ends any time in April.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, +90, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, +3, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
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.
I have a table named dbstorage which has columns like year and weekno.
I need to select the year and weekno which is less than current year and current week.
I used:
select * from dbstorage where year<=2011 and weekno<=13.
But it is not giving the correct data like if I have a value
year:2010 and weekno:25 means, the query is not selecting this record.
How can this be done?
You can split it out into two cases and use or
SELECT * /*<-- But don't use * List the columns explicitly*/
FROM dbstorage
WHERE ( year = 2011
AND weekno <= 13 )
OR year < 2011
Edit
But And logic may well be more efficient
SELECT *
FROM dbstorage
WHERE ( year <= 2011 )
AND ( year < 2011
OR weekno <= 13 )
you have to split up the search into multiple parts:
select * from dbstorage
where
(year = 2011 and weekno <= 13) -- will catch all weeks in 2011 PRIOR to this one
or (year < 2011) -- will catch all weeks in 2010 and earlier
For SQL Server you can also do:
SELECT *
FROM dbstorage
WHERE (year = DATEPART(yyyy, GETDATE())
AND weekno <= DATEPART(ww, GETDATE()))
OR year < DATEPART(yyyy, GETDATE())