How show datetime select only in Sql Server? - sql-server

I need show datetime when I select datetime only. Because I try to run SQL but show all datetime.
Table Emp:
EmpNo fullName
00001 Midna
00002 Klog
00003 Porla
00004 Seka
00005 Mila
Table tFile:
EmpNo cDate cTime
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 19:32:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 18:35:00.000
This code
SELECT
em.EmpNo as 'EmpNo',
case
when tf.cDate <> null then tf.cDate
else coalesce(tf.cDate, '2012-10-29')
end as 'cDate',
Min(tf.cTime) as 'timeIn',
Max(tf.cTime) as 'timeOut'
FROM
tFile tf
Full Outer join
Emp em On tf.EmpNo = em.EmpNo
Group By
em.EmpNo,tf.cDate
Order By
'EmpNo'
returns this result:
EmpNo cDate timeIn timeOut
-------------------------------------------------------------------------------------
00001 2012-10-21 00:00:00.000 2012-10-21 07:22:00.000 2012-10-21 17:35:00.000
00001 2012-10-24 00:00:00.000 2012-10-24 07:30:00.000 2012-10-24 19:00:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-25 00:00:00.000 2012-10-25 07:58:00.000 2012-10-25 18:35:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 08:04:00.000 2012-10-22 17:55:00.000
00002 2012-10-24 00:00:00.000 2012-10-24 08:00:00.000 2012-10-24 18:45:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-28 00:00:00.000 2012-10-28 07:30:00.000 2012-10-28 19:20:00.000
00005 2012-10-27 00:00:00.000 2012-10-27 07:38:00.000 2012-10-27 19:30:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
But I need this result:
I select date ex. 2012-10-29 then I need to show all rows with 2012-10-29 only.
But some Empno don't have data in 2012-10-29 it's set NULL.
EmpNo cDate timeIn timeOut
---------------------------------------------------------------------------------------
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
Thanks for your time. :)

how about this?
where(datediff(dd, coalesce(cTime, getdate()), getdate()) = 0
or, if you really only ever want to look at October 29th:
where(datediff(dd, coalesce(cTime, '2012-10-29'), '2012-10-29') = 0

Related

T-SQL Join two tables where the join condition is dependant on values in both tables

I have a query which combines data from two tables.
Policy table
PolicyID PolicyNumber PolicyStartDate
48 FCO100009 2015-06-01 00:00:00.000
49 FCO100009 2016-06-01 00:00:00.000
Claim Table
ClaimID ClaimReference PolicyNumber IncidentDatetime NotificationDatetime Version
30 287 FCO100009 2015-11-06 00:00:00.000 2015-11-27 00:00:00.000 4. Claim - Incident Date
223 259 FCO100009 2015-11-03 00:00:00.000 2015-11-20 00:00:00.000 4. Claim - Incident Date
1367 988 FCO100009 2016-04-15 00:00:00.000 2016-04-21 00:00:00.000 4. Claim - Incident Date
1561 1859 FCO100009 2016-09-14 00:00:00.000 2016-09-19 00:00:00.000 4. Claim - Incident Date
1741 443275 FCO100009 2016-05-11 00:00:00.000 2016-05-12 00:00:00.000 4. Claim - Incident Date
1742 991 FCO100009 2016-04-20 00:00:00.000 2016-04-21 00:00:00.000 4. Claim - Incident Date
2038 287 FCO100009 2015-11-06 00:00:00.000 2015-11-27 00:00:00.000 5. Claim - Notification Date
3744 259 FCO100009 2015-11-03 00:00:00.000 2015-11-20 00:00:00.000 5. Claim - Notification Date
3745 991 FCO100009 2016-04-20 00:00:00.000 2016-04-21 00:00:00.000 5. Claim - Notification Date
4502 1859 FCO100009 2016-09-14 00:00:00.000 2016-09-19 00:00:00.000 5. Claim - Notification Date
4639 988 FCO100009 2016-04-15 00:00:00.000 2016-04-21 00:00:00.000 5. Claim - Notification Date
6600 443275 FCO100009 2016-05-11 00:00:00.000 2016-05-12 00:00:00.000 5. Claim - Notification Date
There are 2 records for the Policy with different Policy Start Dates and a 2 versions of each claim record where the Version field is either Claim Incident Date or Claim Notification Date.
What I am attempting to accomplish is joining the two tables on PolicyNumber and then setting the PolicyStartDate value in the results to be the maximum value from Policy.PolicyStartDate where the PolicyStartDate is less than the NotificationDate when Version = NotificationDate OR PolicyStartDate is less than the Incident Date when Version = IncidentDate.
Please note that this is using financial NOT Calendar years and in this case the year of account runs from April to March.
Here is my current query which doesn't produce the correct answer:
SELECT cds.ClaimID,
cds.ClaimReference,
p.policyID,
p.PolicyStartDate,
cds.IncidentDatetime,
cds.NotificationDatetime,
cds.[Version]
FROM dbo.ClaimDataStaging cds
INNER JOIN dbo.[Policy] p
ON p.PolicyNumber = cds.PolicyNumber
AND p.PolicyStartDate < CASE WHEN cds.[Version] = '4. Claim - Incident Date' THEN cds.IncidentDatetime
WHEN cds.[Version] = '5. Claim - Notification Date' THEN cds.NotificationDatetime END
WHERE cds.PolicyNumber = 'FCO100009'
ORDER BY cds.[Version], cds.ClaimReference;
GO
Any help or advice much appreciated.
As far as I understood your question and your problem (duplicates caused by condition on date < date which catches former policy too), you can try this query too.
It use LEAD() function to calculate a sort of Enddate (I used IS NULL to to catch last policy, but you can change it adapting to your needs).
I moved the CASE in an inner query to avoid repeating of it in the WHERE clause.
SELECT cds.ClaimID,
cds.ClaimReference,
p.policyID,
p.PolicyStartDate,
cds.IncidentDatetime,
cds.NotificationDatetime,
cds.[Version]
FROM (SELECT ClaimID, ClaimReference, IncidentDatetime,NotificationDatetime,[Version], PolicyNumber
, CASE WHEN [Version] = '4. Claim - Incident Date' THEN IncidentDatetime
WHEN [Version] = '5. Claim - Notification Date' THEN NotificationDatetime END AS CheckDate
FROM dbo.ClaimDataStaging) cds
INNER JOIN (SELECT policyID, PolicyNumber, PolicyStartDate
, LEAD(PolicyStartDate) OVER (PARTITION BY PolicyNumber ORDER BY PolicyStartDate) AS PolicyEndDate FROM dbo.Policy ) p
ON p.PolicyNumber = cds.PolicyNumber
AND p.PolicyStartDate < CheckDate
AND (p.PolicyEndDate IS NULL OR p.PolicyEndDate>=CheckDate)
WHERE cds.PolicyNumber = 'FCO100009'
ORDER BY cds.[Version], cds.ClaimReference;
Output:
+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+
| ClaimID | ClaimReference | policyID | PolicyStartDate | IncidentDatetime | NotificationDatetime | Version |
+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+
| 223 | 259 | 48 | 2015-06-01 00:00:00.000 | 2015-11-03 00:00:00.000 | 2015-11-20 00:00:00.000 | 4. Claim - Incident Date |
| 30 | 287 | 48 | 2015-06-01 00:00:00.000 | 2015-11-06 00:00:00.000 | 2015-11-27 00:00:00.000 | 4. Claim - Incident Date |
| 1367 | 988 | 48 | 2015-06-01 00:00:00.000 | 2016-04-15 00:00:00.000 | 2016-04-21 00:00:00.000 | 4. Claim - Incident Date |
| 1742 | 991 | 48 | 2015-06-01 00:00:00.000 | 2016-04-20 00:00:00.000 | 2016-04-21 00:00:00.000 | 4. Claim - Incident Date |
| 1561 | 1859 | 49 | 2016-06-01 00:00:00.000 | 2016-09-14 00:00:00.000 | 2016-09-19 00:00:00.000 | 4. Claim - Incident Date |
| 1741 | 443275 | 48 | 2015-06-01 00:00:00.000 | 2016-05-11 00:00:00.000 | 2016-05-12 00:00:00.000 | 4. Claim - Incident Date |
| 3744 | 259 | 48 | 2015-06-01 00:00:00.000 | 2015-11-03 00:00:00.000 | 2015-11-20 00:00:00.000 | 5. Claim - Notification Date |
| 2038 | 287 | 48 | 2015-06-01 00:00:00.000 | 2015-11-06 00:00:00.000 | 2015-11-27 00:00:00.000 | 5. Claim - Notification Date |
| 4639 | 988 | 48 | 2015-06-01 00:00:00.000 | 2016-04-15 00:00:00.000 | 2016-04-21 00:00:00.000 | 5. Claim - Notification Date |
| 3745 | 991 | 48 | 2015-06-01 00:00:00.000 | 2016-04-20 00:00:00.000 | 2016-04-21 00:00:00.000 | 5. Claim - Notification Date |
| 4502 | 1859 | 49 | 2016-06-01 00:00:00.000 | 2016-09-14 00:00:00.000 | 2016-09-19 00:00:00.000 | 5. Claim - Notification Date |
| 6600 | 443275 | 48 | 2015-06-01 00:00:00.000 | 2016-05-11 00:00:00.000 | 2016-05-12 00:00:00.000 | 5. Claim - Notification Date |
+---------+----------------+----------+-------------------------+-------------------------+-------------------------+------------------------------+
Based on the suggestions received, I was able to adjust my query to provide the required result. Here is my final code for reference:
SELECT DISTINCT
cds.ClaimID,
MAX(p.PolicyID) OVER (PARTITION BY cds.PolicyNumber) AS PolicyID,
MAX(p.PolicyStartDate) OVER (PARTITION BY cds.PolicyNumber) AS
PolicyStartDate, cds.ClaimKey, cds.ClaimReference, cds.ClaimStatus,
cds.IncidentDatetime, cds.NotificationDatetime, cds.UW_Date,
cds.IncidentType, cds.IncidentDescription, cds.OwnDamagePaid,
cds.OwnDamageReserve, cds.OwnDamageIncurred, cds.TPDamagePaid,
cds.TPDamageReserve, cds.TPDamageIncurred,
cds.BodilyInjuryPaid, cds.BodilyInjuryReserve,
cds.BodilyInjuryIncurred, cds.TotalPaid, cds.TotalReserve,
cds.EstimatedRecovery, cds.ActualRecovery, cds.TotalIncurred, cds.TotalIncurredBand,
CONVERT(VARCHAR(16), 'Current Period') AS TimeView, 1 AS ClaimCount, CONVERT(VARCHAR(48), [Version]) AS [Version]
FROM dbo.ClaimDataStaging cds
INNER JOIN dbo.UW_Calendar u
ON u.UW_Date = cds.UW_Date
LEFT OUTER JOIN dbo.[Policy] p
ON p.PolicyNumber = cds.PolicyNumber
AND p.PolicyStartDate <= CASE
WHEN cds.[Version] = '4. Claim - Incident Date' THEN cds.IncidentDatetime
WHEN cds.[Version] = '5. Claim - Notification Date' THEN cds.NotificationDatetime
END;
GO
I think you're looking for something like the query below. It will give you the max policy number based on the filter you had for the dates.
SELECT cds.ClaimID,
cds.ClaimReference,
p.policyID,
p.PolicyStartDate,
cds.IncidentDatetime,
cds.NotificationDatetime,
cds.[Version]
FROM dbo.ClaimDataStaging cds
CROSS APPLY (
SELECT PolicyStartDate = MAX(fp.PolicyStartDate)
FROM dbo.[Policy] fp
WHERE fp.PolicyNumber = cds.PolicyNumber
AND ((fp.PolicyStartDate < cds.IncidentDatetime AND cds.[Version] = '4. Claim - Incident Date')
OR (fp.PolicyStartDate < cds.NotificationDatetime AND cds.[Version] = '5. Claim - Notification Date')) sp
INNER JOIN dbo.[Policy] p ON p.PolicyNumber = cds.PolicyNumber AND p.PolicyStartDate = sp.PolicyStartDate
WHERE cds.PolicyNumber = 'FCO100009'
ORDER BY cds.[Version], cds.ClaimReference;
GO

Data according to date in MSSQL

I have a data like this
RID Region StartDate EndDate
944 Canada 2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955 Canada 2016-01-17 00:00:00.000 2016-01-24 23:59:59.000
981 Canada 2016-02-01 00:00:00.000 2016-02-08 23:59:59.000
996 Canada 2016-02-09 00:00:00.000 2016-02-16 23:59:59.000
1006 Canada 2016-01-25 00:00:00.000 2016-01-31 23:59:59.000
1020 Canada 2016-02-17 00:00:00.000 2016-02-24 23:59:59.000
1030 Canada 2016-02-25 00:00:00.000 2016-02-29 23:59:59.000
1041 Canada 2016-03-01 00:00:00.000 2016-03-08 23:59:59.000
1046 Canada 2016-03-09 00:00:00.000 2016-03-16 23:59:59.000
1062 Canada 2016-03-17 00:00:00.000 2016-03-24 23:59:59.000
1073 Canada 2016-03-24 00:00:00.000 2016-03-31 23:59:59.000
1083 Canada 2016-04-01 00:00:00.000 2016-04-08 23:59:59.000
1105 Canada 2016-04-09 00:00:00.000 2016-04-16 23:59:59.000
1118 Canada 2016-04-17 00:00:00.000 2016-04-24 23:59:59.000
1128 Canada 2016-04-25 00:00:00.000 2016-04-30 23:59:59.000
1164 Canada 2016-05-01 00:00:00.000 2016-05-08 23:59:59.000
now i try to select data like this
select * from tab1 where Region='Canada'
and StartDate ='2016-01-09 00:00:00.000'
and EndDate ='2016-01-24 23:59:59.000'
desired result is
RID Region StartDate EndDate
944 Canada 2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955 Canada 2016-01-17 00:00:00.000 2016-01-24 23:59:59.000
but when i execute this query data is empty
any solution?
I think you were intending to restrict to a date range, but you actually restricted to two points in time instead. Try this query:
SELECT *
FROM tab1
WHERE Region = 'Canada' AND
StartDate >= '2016-01-09 00:00:00.000' AND
EndDate <= '2016-01-24 23:59:59.000'
Try this.
SELECT *
FROM tab1
WHERE Region = 'Canada'
AND StartDate >='2016-01-09 00:00:00.000'
AND EndDate <='2016-01-24 23:59:59.000'
The 'between' must work. I tried this. If in case it is not working, try convert function for those datetime columns.
SELECT *
FROM tab1
WHERE Region = 'Canada' AND
StartDate >= convert(datetime,'2016-01-09 00:00:00.000') AND
EndDate <= convert(datetime,'2016-01-24 23:59:59.000')

Recursive CTE to split date range

I need to extract and split data from a membership table.
I want to split the range to get one line per year.
DateFrom and dateTo can be any day of the year but when dates are split, we assume that a row ends on december 31 and a new row start on january 1st
Here's a look of the data
membershipId - groupId - ClientId - DateFrom - DateTo
2707 20008 1579 1997-01-01 00:00:00.000 1997-12-31 00:00:00.000
20989 20008 1579 1999-01-01 00:00:00.000 2004-12-31 00:00:00.000
39874 20298 1579 2005-01-01 00:00:00.000 2008-12-31 00:00:00.000
50295 21661 1579 2009-01-01 00:00:00.000 2009-12-31 00:00:00.000
50988 20399 1579 2010-01-01 00:00:00.000 2010-12-31 00:00:00.000
52378 21661 1579 2011-01-01 00:00:00.000 2013-12-31 00:00:00.000
57274 21660 1579 2014-01-01 00:00:00.000 3000-01-01 00:00:00.000
The expected result is : (every range split)
2707 20008 1579 1997-01-01 00:00:00.000 1997-12-31 00:00:00.000
20989 20008 1579 1999-01-01 00:00:00.000 1999-12-31 00:00:00.000
20989 20008 1579 2000-01-01 00:00:00.000 2000-12-31 00:00:00.000
20989 20008 1579 2001-01-01 00:00:00.000 2001-12-31 00:00:00.000
20989 20008 1579 2002-01-01 00:00:00.000 2002-12-31 00:00:00.000
20989 20008 1579 2003-01-01 00:00:00.000 2003-12-31 00:00:00.000
20989 20008 1579 2004-01-01 00:00:00.000 2004-12-31 00:00:00.000
50295 21661 1579 2009-01-01 00:00:00.000 2009-12-31 00:00:00.000
50988 20399 1579 2010-01-01 00:00:00.000 2010-12-31 00:00:00.000
52378 21661 1579 2011-01-01 00:00:00.000 2011-12-31 00:00:00.000
52378 21661 1579 2012-01-01 00:00:00.000 2012-12-31 00:00:00.000
52378 21661 1579 2013-01-01 00:00:00.000 2013-12-31 00:00:00.000
57274 21660 1579 2014-01-01 00:00:00.000 3000-01-01 00:00:00.000
I tried to use recursive CTE based on this :
Possible recursive CTE query using date ranges
But I cannot achieve the desired result.
I made this query :
WITH splitDates(startDate,endDate, newDate,client, groupingId ) as
(
SELECT m.datefrom as startDate, m.dateTo
, CASE
when year(m.dateFrom) <> year(m.dateto) then CAST(CAST(year(m.dateFrom) AS varchar) + '-' + CAST(12 AS varchar) + '-' + CAST(31 AS varchar) AS DATETIME)
else m.dateTo
end
, m.legalEntityId, m.groupingId
from adesse.dbo.membership m
UNION ALL
SELECT DATEADD(year, 1, startDate),
CAST(CAST(year(startDate)+1 AS varchar) + '-' + CAST(12 AS varchar) + '-' + CAST(31 AS varchar) AS DATETIME)
,CAST(CAST(year(startDate)+1 AS varchar) + '-' + CAST(12 AS varchar) + '- ' + CAST(31 AS varchar) AS DATETIME)
,client, groupingId
FROM splitDates
WHERE year(startDate) <> year(endDate)
)
SELECT *
FROM splitDates
where client = 1579
order by startDate
But the result is incomplete :(
startDate endDate newDate client groupingId
1997-01-01 00:00:00.000 1997-12-31 00:00:00.000 1997-12-31 00:00:00.000 1579 20008
1999-01-01 00:00:00.000 2004-12-31 00:00:00.000 1999-12-31 00:00:00.000 1579 20008
2000-01-01 00:00:00.000 2000-12-31 00:00:00.000 2000-12-31 00:00:00.000 1579 20008
2005-01-01 00:00:00.000 2008-12-31 00:00:00.000 2005-12-31 00:00:00.000 1579 20298
2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 2006-12-31 00:00:00.000 1579 20298
2009-01-01 00:00:00.000 2009-12-31 00:00:00.000 2009-12-31 00:00:00.000 1579 21661
2010-01-01 00:00:00.000 2010-12-31 00:00:00.000 2010-12-31 00:00:00.000 1579 20399
2011-01-01 00:00:00.000 2013-12-31 00:00:00.000 2011-12-31 00:00:00.000 1579 21661
2012-01-01 00:00:00.000 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 1579 21661
2014-01-01 00:00:00.000 3000-01-01 00:00:00.000 2014-12-31 00:00:00.000 1579 21660
2015-01-01 00:00:00.000 2015-12-31 00:00:00.000 2015-12-31 00:00:00.000 1579 21660
Thx for the help
I'm not sure if your last date is suppose to be 3000-01-01 but this should work
CREATE TABLE members (membershipId INT, groupId INT, clientId INT, dateFrom DATETIME, dateTo DATETIME)
INSERT INTO members VALUES
(2707, 20008, 1579, '1997-01-01 00:00:00.000', '1997-12-31 00:00:00.000'),
(20989, 20008, 1579, '1999-01-01 00:00:00.000', '2004-12-31 00:00:00.000'),
(39874, 20298, 1579, '2005-01-01 00:00:00.000', '2008-12-31 00:00:00.000'),
(50295, 21661, 1579, '2009-01-01 00:00:00.000', '2009-12-31 00:00:00.000'),
(50988, 20399, 1579, '2010-01-01 00:00:00.000', '2010-12-31 00:00:00.000'),
(52378, 21661, 1579, '2011-01-01 00:00:00.000', '2013-12-31 00:00:00.000'),
(57274, 21660, 1579, '2014-01-01 00:00:00.000', '3000-01-01 00:00:00.000')
;
WITH cte AS
(
SELECT
membershipId,
groupId,
clientId,
dateFrom,
DATEADD(day, -1, DATEADD(YEAR,1,dateFrom)) newDateTo,
dateTo
FROM
members
UNION ALL
SELECT
m.membershipId,
m.groupId,
m.clientId,
DATEADD(YEAR,1,c.dateFrom),
DATEADD(day, -1, DATEADD(YEAR,2,c.dateFrom)),
c.dateto
FROM
members m
JOIN cte c ON c.membershipId = m.membershipId
AND DATEADD(YEAR,1,c.dateFrom) < m.dateTo
)
SELECT
membershipId,
groupId,
clientId,
dateFrom,
newDateTo dateTo
FROM
cte
ORDER BY
membershipId, dateFrom
OPTION (MAXRECURSION 0);
DROP TABLE members
SQL Fiddle

How to SELECT the last distinct record in SQL SERVER

So, I have this table:
| COD | | ID | |BALANCE| | DATE |
20002978 34134987 2,1 2012-10-20 00:00:00.000
20002978 34134987 2,1 2012-10-30 00:00:00.000
20002978 34134987 10,1 2012-12-05 00:00:00.000
20002978 34134987 8,1 2012-12-22 00:00:00.000
20002978 34134987 9,1 2013-01-16 00:00:00.000
20002978 34134987 23,1 2013-01-19 00:00:00.000
20002978 34134987 7,1 2013-01-29 00:00:00.000
20002978 34134987 3,1 2013-02-02 00:00:00.000
80125573 34134987 13,1 2013-02-22 00:00:00.000
80125573 34134987 1,0 2013-03-08 00:00:00.000
I want to select the last balance of the last code where the ID is the parameter that I'll pass to the procedure.
I need something like this as the result:
ID = 34134987
| COD | | ID | |BALANCE| | DATE |
20002978 34134987 3,1 2013-02-02 00:00:00.000
80125573 34134987 1,0 2013-03-08 00:00:00.000
Any tips? Thanks in advance.
Try like this.....
Select Cod,Id,Balance,[Date] from (
Select Row_Number() Over(Partition By Cod,Id Order By [Date] desc) as Row, Cod,Id,Balance,Date
from table) t where t.Row=1

How can I update from select other table?

i need update fields 'ErrCode' from select between 2 table. i try query it's update all record.
Table LeaveEmp
EmpRun Date Start Stop
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000
00004 2012-10-02 00:00:00.000 NULL NULL
Table modifyTime
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL NULL
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 NULL
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 NULL
00004 2012-10-02 00:00:00.000 NULL NULL NULL
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 NULL
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 NULL
*****00003,00004 from Table LeaveEmp.step one i insert Table LeaveEmp to Table modifyTime**
code:
UPDATE modifyTime
SET ErrCode = '3'
WHERE EXISTS (SELECT coalesce(l.Start,'2012-10-02')
FROM LeaveEmp l
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02')
this get output:
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL 3
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 3
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 3
00004 2012-10-02 00:00:00.000 NULL NULL 3
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 3
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 3
but i need output:
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL NULL
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 NULL
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 3
00004 2012-10-02 00:00:00.000 NULL NULL 3
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 NULL
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 NULL
i need update from Table LeaveEmp only.
Thanks you fou your time. :)
You are missing a link between modifyTime and LeaveEmp... try this
UPDATE modifyTime
SET ErrCode = '3'
FROM modifyTime m
INNER JOIN LeaveEmp l
ON m.EmpNo = l.EmpRun
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02'

Resources