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
 ? about LIKE statement

Author  Topic 

rnonajr
Starting Member

1 Post

Posted - 2009-12-17 : 11:56:45
I am trying to get all the data from the Spreads table where the Month of the meeting date in the contracts table is in the Months of the Spreads table the and the ContractYR are equal. When I run my queries separately they work fine, but when I run the combined it doesn't work. I'm sure it has something to do with the syntax but I can't figure it out.
Following is my code and below that is a data example.
select * from Spreads 
where Months LIKE
(SELECT
CAST(MONTH
(Meeting_Date) AS CHAR) FROM
Contracts WHERE Meeting_Date IS NOT null)


Here is some data. My Spreads table

ContractName ContractYR Months
Butterfly: 1xZQ Apr10:-2xZQ May10:1xZQ Jun10 10 4 5 6
Butterfly: 1xZQ May10:-2xZQ Jun10:1xZQJul10 10 5 6 7
Butterfly: 1xZQ Apr11:-2xZQ May11:1xZQ Jun11 11 4 5 6

10 and 11 are the contract years and my months column is of type CHAR now.

Here is my Contracts table
Contract Contract YR Meeting Date
ZQMar10 10 NULL
ZQApr10 10 NULL
ZQMay10 10 2010-05-21

So in this instance I should get back basically all the info associated with the 1st two records of my spreads table because they have Months that are equal to 5 and their contract years are equal.

I hope someone can help me.

Thanks
Bob

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-17 : 12:10:49
Try this
select a.*
from Spreads a
inner join Contracts b on a.ContractYR = b.[Contract YR] and a.Months = month(b.[Meeting Date])

Go to Top of Page
   

- Advertisement -