SQL String Format - Returns No Data

Posted on 2011-03-13
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.
Question by:_Carthmen_
  • 2
  • 2

Expert Comment

ID: 35122867
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.


Author Comment

ID: 35123135
they contain a string, with a date formated at MM/DD/YYYY (VARCHAR) no time

Accepted Solution

robbert_nl earned 500 total points
ID: 35123185
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`

Author Closing Comment

ID: 35125233
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.

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

919 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

17 Experts available now in Live!

Get 1:1 Help Now