Solved

SQL String Format - Returns No Data

Posted on 2011-03-13
4
352 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
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.



0
 

Author Comment

by:_Carthmen_
ID: 35123135
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
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`
0
 

Author Closing Comment

by:_Carthmen_
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.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Read about achieving the basic levels of HRIS security in the workplace.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

809 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