Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Finding the record that between two dates

Author  Topic 

stockcer
Starting Member

1 Post

Posted - 2013-07-30 : 11:06:18
Let me explain.

table 1

David 05/10/2013
Peter 05/16/2013

table 2
Period_Num Begin_Period End_Period
Period 1 01/05/2013 05/15/2013
Period 2 05/16/2013 12/31/2013


I want the final to read

David Period 1
Peter Period 2

Is there a way to do this in sql particularly in a view

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-30 : 12:36:59
SELECT *
FROM dbo.Table2 AS t2
INNER JOIN dbo.Table1 AS t1 ON t2.Col2 BETWEEN t1.Begin_Period AND t1.End_Period



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-30 : 12:37:03
[CODE]


DECLARE @Temp1 TABLE (NAME VARCHAR(16), Period DATE);
DECLARE @Temp2 TABLE (Period VARCHAR(16), Begin_Period DATE, End_Period DATE);

INSERT INTO @Temp1 VALUES
('David', '2013-05-10'),
('Peter', '2013-05-16');

INSERT INTO @Temp2 VALUES
('Period1', '2013-01-05', '2013-05-15'),
('Period2', '2013-05-16', '2013-12-31');

SELECT Name, (SELECT Period from @Temp2 where T1.period between Begin_Period and End_Period) as Period from @Temp1 T1;


[/CODE]
Go to Top of Page
   

- Advertisement -