Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Datetime Existence within multiple columns Expand / Collapse
Author
Message
Posted Wednesday, January 23, 2013 5:05 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 23, 2013 5:55 AM
Points: 9, Visits: 9
Hi there,

I've a BillDate as date, and a Mark as bit column in First table.
(Mark=0 by default)

In Second table I've FromDate as date, and ToDate as date Column.

I want to set Mark=1 if BillDate is exists between FromDate & ToDate

Let Say In First Table the data is
----------------------------
BillDate | Mark
----------------------------
2012-11-10 11:15:30 | 0
2012-12-12 09:00:00 | 0

In Second Table the data is
---------------------------------------------
FromDate | ToDate
---------------------------------------------
2012-11-01 07:00:00 | 2012-11-09 23:59:59
2012-12-08 07:00:00 | 2012-12-15 23:59:59

So in the above scenario only the second row from First table
which is having, BillDate->2012-12-12 09:00:00 will be Mark as 1
because it comes between second row of second table

I hope I've explained my scenario,
Post #1410489
Posted Wednesday, January 23, 2013 5:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
Please take a moment to read through the second link in my signature on posting code and data for the best help.

But based on your post, you need something like this

[cod="sql"]
DECLARE @Bill TABLE (BillDate DATETIME, Mark BIT)
INSERT INTO @Bill VALUES
('2012-11-10 11:15:30',0),
('2012-12-12 09:00:00',0)

DECLARE @Dates TABLE (FromDate DATETIME, ToDate DATETIME)
INSERT INTO @Dates VALUES
('2012-11-01 07:00:00','2012-11-09 23:59:59'),
('2012-12-08 07:00:00','2012-12-15 23:59:59')

UPDATE
B
SET
Mark = 1
FROM
@Bill B
INNER JOIN
@Dates d
ON
b.BillDate >= d.FromDate
AND
b.BillDate <= d.ToDate

SELECT * FROM @Bill
[/code]




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1410513
Posted Wednesday, January 23, 2013 5:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 23, 2013 5:55 AM
Points: 9, Visits: 9
Thank you so much Tunnel.

I've tried it and its working fine.

How can I mark your post as an answer?
Post #1410520
Posted Wednesday, January 23, 2013 6:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
You cannot mark them as answered on SSC, as it is primarily a discussion forum so topics are open for all to see and people may come back with their own take on how to solve your problem.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1410521
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse