SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Finding the record that between two dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stockcer
Starting Member

1 Posts

Posted - 07/30/2013 :  11:06:18  Show Profile  Reply with Quote
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

Sweden
29910 Posts

Posted - 07/30/2013 :  12:36:59  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

547 Posts

Posted - 07/30/2013 :  12:37:03  Show Profile  Reply with Quote



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;


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000