Comparing two DATETIME columns in SQL - sql-server

I have two datetime columns:
StartDate : 2019-07-01 13:30:00.000
AdmitDate : 2019-07-01 00:00:00.000
I want to select all rows that have the same date (excluding the time, seconds, etc).
SELECT *
FROM table1 as t1
INNER JOIN table2 as t2 ON t1.id = t2.id
WHERE t1.startdate >= t2.admitdate or t1.startdate <= t2.admitdate
However, this won't work because one of the columns has hours, minutes and seconds. How to fix this? Again, I just want the date, not time.

The solution is simpler to convert the columns in Date
it depends if both columns are datetime or one is date
SELECT Table1.Id,Table1.Startdate
FROM Table1
INNER JOIN Table2 ON Table1.Id = Table2.Id
where (cast(Table1.Startdate as date))=(cast(Table2.AdmitDateTime as date))
SELECT Table1.Id,Table1.Startdate
FROM Table1
INNER JOIN Table2 ON Table1.Id = Table2.Id
where (cast(Table1.Startdate as date))=Table2.AdmitDate
Example

How about using DATEDIFF()?
SELECT *
FROM table1 as t1
INNER JOIN table2 as t2 ON t1.id = t2.id
WHERE DATEDIFF(DAY, t1.startdate, t2.admitdate) = 0

Related

SQL Server 2012 query date

How can I return values in a where clause for something like this:
Get me all the records that exist in table1.de1,table2.de2,table3.de3,table4.de3
select *
from table1
inner join table2
on table2.carID = table1.carID
inner join table3
on table3.carID = table1.carID
inner join table4
on table4.driverID = table1.driverID
where a recietrecord exists in table2 and its paydate has passed 20 days ago, comparing it to TODAYS date and show those days in a field called Days Passed From The Day Driver Was Suppose To Pay
At first, please, use table aliases. This solution is for SQL Server:
As was written in comments you can use DATEDIFF function to compare paydate to GETDATE.
select *,
DATEDIFF(day,t2.paydate,GETDATE()) as [Days Passed From The Day Driver Was Suppose To Pay]
from table1 t1
inner join table2 t2
on t2.carID = t1.carID
inner join table3 t3
on t3.carID = t1.carID
inner join table4 t4
on t4.driverID = t1.driverID
WHERE DATEDIFF(day,t2.paydate,GETDATE()) > 20
Or better use minutes:
DATEDIFF(minute,t2.paydate,GETDATE()) > 28800 --60 minutes * 24 hours * 20 days

sql server left join with filter - sql server 2012

I have 2 tables . One of employees and other breaks. I want to show all records from the Employees table ( LEFT JOIN) and see if any employee rests next Sunday (if have a record in the other table - breaks
SELECT T1.idEmp
, T1.fname
, T1.lname
, T1.number
, T2.idGuard
, T2.date
FROM
tblEmp AS T1 LEFT JOIN tblEmpGuard AS T2
ON
T1.idEmp = T2.idEmp
WHERE
date = #date
this just shows me employees having break next Sunday , but not the others.
Try
SELECT T1.idEmp
, T1.fname
, T1.lname
, T1.number
, T2.idGuard
, T2.[date]
FROM tblEmp AS T1
LEFT JOIN tblEmpGuard AS T2
ON T1.idEmp = T2.idEmp
AND T2.[date] = #date
This is because the where clause is filtering for employees with a break on that date. This occurs after the join thus excluding employees without dates. Moving this criteria to the on clause will solve the problem.
SELECT T1.idEmp, T1.fname, T1.lname, T1.number, T2.idGuard, T2.date
FROM tblEmp AS T1
LEFT JOIN tblEmpGuard AS T2
ON T1.idEmp = T2.idEmp
AND T2.date = #date

How to use SQL(Max) function

I have 2 tables
Table 1:
id name adress
1 John New York
2 Jane London`
... and so on
Table 2:
id fila date
1 43 01/01/2010
1 39 10/01/2011
1 55 23/12/2012
2 10 01/01/2008
2 15 02/02/2010`
.... and so on
I want to get data like this
id fila name adress date
-----------------------------------------
1 55 John New York 23/12/2012
2 15 Jane London 02/02/2010
..... and so on.
Thanks
ok. what you are really looking for is "What is the latest date in table2 for each of my rows in Table1". So to answer the question:
select *
From Table1
inner join (
select id, max(fila) as maxfila
from Table2
group by id
) as maxdates
on Table1.id = maxdates.id
inner join Table2 on Table2.id = maxdates.id AND Table2.fila = maxdates.maxfila
Try this:
;with cte as
(select id, max(fila) maxfila
from table2
group by id)
select t1.id, t1.name, t1.address, t2.fila, t2.date
from table1 t1
left join table2 t2 on t1.id = t2.id
inner join cte c on t1.id = c.id
where t2.fila = c.maxfila
Try this
Select t1.id, t1.name, t1.address, t2.maxfila
from table1 t1
left outer join
(select id, max(fila) maxfila
from table2
group by id) t2
select t1.id, t1.name t1.address, max(t2.fila),
(select top 1 date from table2 order by fila desc where table2.id = t1.id)
from table1 t1 inner join
table2 t2 on t1.id = t2.id

T- SQL / Find total time pending with multiple over lapping pend tasks / Need to ignore overlap and weekend days

I am hoping for some help in removing weekends from this query. I have cases that have multiple pend tasks that can overlap in time. This query works, but I was hoping to remove weekends.
DECLARE #pends table (id varchar(50), d1 datetime, d2 datetime);
INSERT #pends
--- Play below me --- This is the query that pulls the pend tasks.
SELECT
dc.fldReferenceNumber,
dt.fldCreatedTime,
dt.fldDoneDate
FROM ([Centerpoint_Reporting_Prod].[dbo].[dim_case] dc
INNER JOIN ([Centerpoint_Reporting_Prod].[dbo].[dim_task] dt
INNER JOIN ([Centerpoint_Reporting_Prod].[dbo].[fact_task] ft
INNER JOIN [Centerpoint_Reporting_Prod].[dbo].[dim_task_type] dtt
ON ft.fldTaskTypeIdTaskTypeKey = dtt.TaskTypeSK)
ON ft.TaskSK = dt.TaskSK)
ON ft.CaseKey = dc.CaseSK)
WHERE dtt.Name_enUS LIKE 'Pending%' AND dt.fldDoneDate <> 0 and dc.fldReferenceNumber = 'CDCR-4998513'
--- Play above me ---
SELECT id, SUM (n) AS hold_days
FROM (
SELECT DISTINCT
t1.id,
t1.d1 AS date,
-DATEDIFF(DAY, (SELECT MIN(d1) FROM #pends), t1.d1) AS n
FROM #pends t1
LEFT JOIN #pends t2
ON t2.ID = t1.ID
AND t2.d1 <> t1.d1
AND t1.d1 BETWEEN t2.d1 AND t2.d2
GROUP BY t1.ID, t1.d1, t1.d2
HAVING COUNT(t2.ID) = 0
UNION ALL
SELECT DISTINCT
t1.id,
t1.d2 AS date,
DATEDIFF(DAY, (SELECT MIN(d1) FROM #pends), t1.d2) AS n
FROM #pends t1
LEFT JOIN #pends t2
ON t2.ID = t1.ID
AND t2.d2 <> t1.d2
AND t1.d2 BETWEEN t2.d1 AND t2.d2
GROUP BY t1.ID, t1.d1, t1.d2
HAVING COUNT(t2.ID) = 0
) s
GROUP BY ID;
Look into DATENAME in bol, there is also a function that returns the day of the week 1-7 where 1 is sunday and 7 is saturday.
DATENAME ( datepart , date )

Opposite Of An Inner Join Query

Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);

Resources