SQL query, information in a specific period from current date - sql

Hi guys hope you can help, I've done a search but can't seem to find what I'm looking for...
I'm trying to write a SQL query to show me all employees that have started 12 weeks or 84 days from the date of running the query (current date). I've got this so far but it's not returning the results I was expecting, I'm getting some records through where the start date is older than 84 days.
.... WHERE (EmployeeJobs.DateTo IS NULL or EmployeeJobs.DateTo = dateadd(d,datediff(d,0,getdate()),84))
I need a NULL statement included as unfortunately not all the records have a date in them.
Any help would be much appreciated

where
EmployeeJobs.DateTo IS NULL
or
datediff(d, EmployeeJobs.DateTo, getdate()) = 84
If you're using SQL Server 2008, then
where
EmployeeJobs.DateTo IS NULL
or
cast(EmployeeJobs.DateTo as date) = cast(getdate() - 84 as date)

Try it like this:
WHERE
(EmployeeJobs.DateTo IS NULL
or
convert(varchar, EmployeeJobs.DateTo, 112) = convert(varchar, DATEADD(day, -84, getdate(), 112)
)

Try this one, its suitable for all versions of Sql Server 2000+
....
WHERE
(EmployeeJobs.DateTo IS NULL
or
CONVERT(varchar, EmployeeJobs.DateTo, 112) = CONVERT(varchar, dateadd(d,-84,GETDATE()), 112)
)
BUT if you running it # sql server 2008+ you can simplify the work:
....
WHERE
(EmployeeJobs.DateTo IS NULL
or
cast(EmployeeJobs.DateTo as DATE) = CAST(dateadd(d,-84,GETDATE()) as DATE)
)
OR with the possibility of using index
....
WHERE
(EmployeeJobs.DateTo IS NULL
or
EmployeeJobs.DateTo >= CAST(dateadd(d,-84,GETDATE()) as DATE)
AND
EmployeeJobs.DateTo < CAST(dateadd(d,-83,GETDATE()) as DATE)
)

Do you have a DateFrom and a DateTo field? And the range of those two dates shows that they were employed between those two dates? Where a NULL DateTo means they have not yet left? And you want people who were employed 84 days ago?
WHERE
(EmployeeJobs.DateFrom <= dateadd(d, datediff(d,0,getdate()) - 84, 0))
AND (EmployeeJobs.DateTo >= dateadd(d, datediff(d,0,getdate()) - 84, 0) OR EmployeeJobs.DateTo IS NULL)

Alternatively (as functions are used already the impact on performance is there):
WHERE
-- get difference in days
DateDiff(dd,
-- lower date, replace Nulls
IsNull(EmployeeJobs.DateTo, GetDate()),
-- highest date
GetDate()
)
-- whatever the criteria, in this case equal to exact 84 days
= 84

Related

SQL, GETDATE in where clause

I am trying to use my query as the current day for my SQL query.
What I am trying below returns 0 records:
select *
from [TEST].[dbo].LIMIT
where endOfDay = GETDATE()
select *
from [TEST].[dbo].LIMIT
where endOfDay = dateadd(dd, datediff(dd, 0, getdate()), 0)
where endOfDay >= getdate() and endOfDay < getdate() + 1
An example date would be 2019-07-09 00:00:00.0
Also if there is no given date for today's date can I get a range of dates?
Any help would be appreciated, thanks.
First, you need to omit the quotes or else 'GETDATE()' is just a string.
Second, to match by "day", you need to strip the time part from the result of GETDATE(), which you can do by using CAST(GETDATE() as DATE) instead:
select *
from [TEST].[dbo].LIMIT
where endOfDay = CAST(GETDATE() as DATE)
For this to work best, the endOfDay column also needs to be of type DATE. If it is something else, you need to also CAST or CONVERT endOfDay to a DATE.
Using quotes makes it looking for a meaningless literal 'GETDATE()' to find nothing
You can use
with LIMIT(today, EndOfDay) as
(
select CONVERT(DATETIME, CONVERT(date, GETDATE())), GETDATE()
)
select EndOfDay
from LIMIT
where EndOfDay >= today and EndOfDay < today + 1;
Demo
Try this:
select *
from [TEST].[dbo].LIMIT
where cast(endOfDay as date) = cast(GETDATE() as date)
SQL Fiddle to see what the date will be compared with.
GETDATE() returns the server's current date and time. Unless you have any records that match that exactly, it doesn't make sense to use GETDATE. It doesn't look like you do based on the endOfDay column name.
The other criteria you show,
dateadd(dd,datediff(dd,0,getdate()),0) is essentially stripping the time off and returning midnight of the current date.
select getdate() 2019-07-11 15:10:09.287
select dateadd(dd,datediff(dd,0,getdate()),0) 2019-07-11 00:00:00.000
Assuming you don't care about time, convert both EndofDay and Getdate to a simple date for comparison.
select * from [TEST].[dbo].LIMIT where convert(date,EndOfDay) = convert(date,getdate())

Anything that expected within the last 10 days back should be received if not ignore them in SQL

I need to ignore data anything that expected within the last 10 days back should be received if not ignore it. I am not sure how to write this in SQL. I have EXPDATE column. I am not sure my statement correct or not.
I believe the logic should be like this
Expected Date + 10 Days < Today`s date?
GETDATE() < DATEADD(DAY, +10, GETDATE()) - I found this online but where can I plug in my ExpectedDate column?
Thanks in advance!!
You can do it either way-
First add 10 days to EXPDATE and compare it with todays date like below.
select * from MyTable Where DATEADD(DAY, 10, EXPDATE) < GETDATE()
The other way, you can substract 10 days from today and compare it with EXPDATE.
select * from MyTable Where DATEADD(DAY, -10, GETDATE()) > EXPDATE
I would prefer 2nd one and would use variable to calculate 10 days back date as it's constant and then use it in where clause like below.
Declare #myDate datetime
SET #myDate = DATEADD(DAY, -10, GETDATE())
select * from MyTable Where #myDate > EXPDATE

SQL Server date setting

running to a small issue.
I have a scrpit that based on date. what i need is to retrieve data that is 14 days ago not including the date it was run.
example; date run 12/15/2016 should retrieve Data between 12/1/2016 - 12/14/2016.
The issue i have is it ends with the date was run. see below the criteria I set.
where a.UF_1 <> '' and (DATEDIFF(dd, a.TRANSACTION_DATE, getdate()) <14)
any help is appreciated.
Thank you.
select * from yourTable
where datecolumn < getdate()
and datecolumn >=dateadd(dd,-14,getdate())
OR . . . .
declare #someDateVariable datetime - '12/15/2016'
select *
from yourTable
where cast(datecolumn as date) < cast(#someDateVariable as date)
and datecolumn >= dateadd(dd,-14,#someDateVariable)
Do you mean like this:
where a.UF_1 <> '' and cast(a.TRANSACTION_DATE as date) between cast(getdate()-14 as date) and cast(getdate()-1 as date).
I assume that neither a.UF_1 or a.TRANSACTION_DATE is nullable.. otherwise handle this.

get todays date with last 6 months back

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)

SQL statement to select all rows from previous day

I am looking for a good SQL Statement to select all rows from the previous day from one table. The table holds one datetime column. I am using SQL Server 2005.
get today no time:
SELECT dateadd(day,datediff(day,0,GETDATE()),0)
get yestersday no time:
SELECT dateadd(day,datediff(day,1,GETDATE()),0)
query for all of rows from only yesterday:
select
*
from yourTable
WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)
To get the "today" value in SQL:
convert(date, GETDATE())
To get "yesterday":
DATEADD(day, -1, convert(date, GETDATE()))
To get "today minus X days": change the -1 into -X.
So for all yesterday's rows, you get:
select * from tablename
where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
It's seems the obvious answer was missing. To get all data from a table (Ttable) where the column (DatetimeColumn) is a datetime with a timestamp the following query can be used:
SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- yesterday
This can easily be changed to today, last month, last year, etc.
SELECT * from table_name where date_field = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
Its a really old thread, but here is my take on it.
Rather than 2 different clauses, one greater than and less than. I use this below syntax for selecting records from A date. If you want a date range then previous answers are the way to go.
SELECT * FROM TABLE_NAME WHERE
DATEDIFF(DAY, DATEADD(DAY, X , CURRENT_TIMESTAMP), <column_name>) = 0
In the above case X will be -1 for yesterday's records
This should do it:
WHERE `date` = CURDATE() - INTERVAL 1 DAY
Can't test it right now, but:
select * from tablename where date >= dateadd(day, datediff(day, 1, getdate()), 0) and date < dateadd(day, datediff(day, 0, getdate()), 0)
In SQL Server do like this:
where cast(columnName as date) = cast(getdate() -1 as date)
You should cast both sides of the expression to date to avoid issues with time formatting.
If you need to control interval in more detail, then you should try something like:
declare #start datetime = cast(getdate() - 1 as date)
declare #end datetime = cast(getdate() - 1 as date)
set #end = dateadd(second, 86399, #end)
Another way to tell it "Yesterday"...
Select * from TABLE
where Day(DateField) = (Day(GetDate())-1)
and Month(DateField) = (Month(GetDate()))
and Year(DateField) = (Year(getdate()))
This conceivably won't work well on January 1, as well as the first day of every month. But on the fly it's effective.
Well, its easier to cast the datetime column to date and than compare.
SELECT * FROM TABLE_NAME WHERE cast(COLUMN_NAME as date) =
dateadd(day,0, convert(date, getdate(), 105))
A simple alternative
Select GETDATE() - 1
Change 1 to go back that many number of days
PS : This gives you timestamp accuracy.
This worked a charm:
SELECT * FROM mytable WHERE date(mydate) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp
Select * FROM `login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)