Solved

SQL String Format - Returns No Data

Posted on 2011-03-13
4
349 Views
Last Modified: 2012-05-11
I'm creating a sql query and have managed to get to this so far.

SELECT  `OptionData`.`Symbol` ,  `OptionData`.`Date`, `OptionData`.`Expiration` ,  `OptionData`.`AdjClose` ,  `OptionData`.`Strike` ,  `OptionData`.`Type` , (`OptionData`.`Bid` +  `OptionData`.`Ask`) /2 AS  `Price` , DATEDIFF(  STR_TO_DATE(`OptionData`.`Expiration`,'%m/%d/%Y') ,  STR_TO_DATE(`OptionData`.`Date`,'%m/%d/%Y') ) AS  `DaysToExp`, `IWM`.`Close` AS `ExpClose` FROM  `PriceData`.`OptionData` AS  `OptionData`, `PriceData`.`IWM` AS `IWM` WHERE  `OptionData`.`AdjClose` -  `OptionData`.`Strike` <  '10' AND  `OptionData`.`AdjClose` -  `OptionData`.`Strike` >  '-10' AND DATEDIFF(  STR_TO_DATE(`OptionData`.`Expiration`,'%m/%d/%Y') ,  STR_TO_DATE(`OptionData`.`Date`,'%m/%d/%Y') ) > '15' AND DATEDIFF(  STR_TO_DATE(`OptionData`.`Expiration`,'%m/%d/%Y') ,  STR_TO_DATE(`OptionData`.`Date`,'%m/%d/%Y') ) < '45' AND STR_TO_DATE(`IWM`.`Date`, '%m/%d/%Y') = STR_TO_DATE(`OptionData`.`Expiration`, '%m/%d/%Y')

I want to select from OptionData all records Where Strike Number is within 10 points of the AdjClose, and in the feild 'ExpClose' get the 'close' from the 'IWM' table for that 'Expiration' Date.

The above seems as though it should work, but returns no records.

IF I were to remove the 'IWM' table data it returns records fine.  And there are matching Dates on the two tables.

Hope this is somewhat clear, there is a lot here.
0
Comment
Question by:_Carthmen_
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:robbert_nl
Comment Utility
You say that if the IWM table is empty you get the a result set, so the cause should be found in the last part of the WHERE clause:

STR_TO_DATE(`IWM`.`Date`, '%m/%d/%Y') = STR_TO_DATE(`OptionData`.`Expiration`, '%m/%d/%Y')

What values do these attributes contain? Do they both contain only dates? or also time?

STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE value if the string contains only date part.



0
 

Author Comment

by:_Carthmen_
Comment Utility
they contain a string, with a date formated at MM/DD/YYYY (VARCHAR) no time
0
 
LVL 1

Accepted Solution

by:
robbert_nl earned 500 total points
Comment Utility
When you do these selects you get data and not NULL values?

SELECT STR_TO_DATE(`IWM`.`Date`, '%m/%d/%Y')
FROM `IWM`.`Date`

SELECT STR_TO_DATE(`OptionData`.`Expiration`, '%m/%d/%Y')
FROM `OptionData`.`Expiration`
0
 

Author Closing Comment

by:_Carthmen_
Comment Utility
Thank you so much.  I had a bad import on that table that formatted all the data in the 'date' field wrong,  it was returning data but was formatted very wrong so there were no matching records to have.

Should have noticed that before, thank you very much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now