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
 General SQL Server Forums
 New to SQL Server Programming
 Combining two tables to one

Author  Topic 

akamole
Starting Member

17 Posts

Posted - 2014-01-10 : 09:36:47
Hi

I need to combine data from two tables located in the same database.


The column GID in Table 1 and RealEstateID in Table 2 has a common id

I want to specify a date interval (AcquistitionDate in Table 2) eg 20131001 to 20131030 and get Name from table 1, and combine these to a result table.



Something like this (?):
[url]http://stackoverflow.com/questions/16364187/combining-2-sql-queries-and-getting-result-set-in-one[/url]

singularity
Posting Yak Master

153 Posts

Posted - 2014-01-10 : 09:38:55
[code]
select a.Name, b.AcquisitionDate
from table1 a
join table2 b on a.GID = b.RealEstateID
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-10 : 09:45:37
quote:
Originally posted by singularity


DECLARE @STart datetime,@End datetime
SELECT @STart='20131001'.@End='20131030'

select a.Name, b.AcquisitionDate
from table1 a
join table2 b on a.GID = b.RealEstateID
WHERE b.AcquisitionDate BETWEEN @Start AND @End




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

akamole
Starting Member

17 Posts

Posted - 2014-01-10 : 10:06:53
Thank you, i changed from datetime to nvarchar(8) in DECLARE statement and it worked perfectly!

quote:
Originally posted by visakh16

quote:
Originally posted by singularity


DECLARE @STart datetime,@End datetime
SELECT @STart='20131001'.@End='20131030'

select a.Name, b.AcquisitionDate
from table1 a
join table2 b on a.GID = b.RealEstateID
WHERE b.AcquisitionDate BETWEEN @Start AND @End




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-10 : 23:08:29
why? what you're passing are datetime values so variable should be of type datetime and not nvarchar

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -