Solved

Help converting MSSQL query to MYSQL

Posted on 2014-12-19
4
247 Views
Last Modified: 2015-01-02
MYSQL 5.6.22

   Hi,

       I need some help converting a query for use in MYSQL please.  The code below was written for MSSQL and I already have the joins converted for MYSQL.  One problem is that the 'pat_birthdate' field is 'VARCHAR(250) and not a 'DATE' datatype.  I'm not sure how/if this could be affected, but I'm guessing that the query just might 'miss' those rows if they do not have a proper date.

thank you


select s.study_iuid
from pacsdb.patient p
INNER JOIN study s
on p.pk = s.patient_fk
/***where p.pk = s.patient_fk***/
where s.mods_in_study NOT LIKE '%MG%'
/***and s.mods_in_study NOT LIKE 'RTIMAGE'***/
and s.study_datetime IS NOT NULL
and p.pat_birthdate IS NOT NULL
and ISDATE(p.pat_birthdate) = 1
and ISDATE(s.study_datetime) = 1
and CASE WHEN ISDATE(s.study_datetime) = 1 THEN s.study_datetime END
  <= DATEADD(DAY, -2192, GETDATE())
and CASE WHEN ISDATE(p.pat_birthdate) = 1 THEN p.pat_birthdate END
  <= DATEADD(DAY, -7670, GETDATE());

Open in new window

0
Comment
Question by:doc_jay
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40510417
For starters, MySQL does not have an 'ISDATE' function.  Next, the TSQL DATEADD and the MySQL DATE_ADD use different syntax.  

TSQL DATEADD http://msdn.microsoft.com/en-us/library/ms186819.aspx

MySQL DATE_ADD http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
0
 

Author Comment

by:doc_jay
ID: 40510431
thanks for your comment.  I'm hoping for a little more help with the 'study_datetime' & the pat_birthdate calculation though.

for 'study_datetime' I'm looking for any row that has a date that is older than 2192 days old or 6 years from today & for the 'pat_birthdate' line, any row that has a birthdate that is older than 7670 days.  This would ensure the patient is older than 21 years.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40510467
MySQL Date arithmetic only works on Date/Datetime columns.  It won't work at all on a VARCHAR column.  And where you have GETDATE(), MySQL has NOW() which is documented here: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_now  'STR_TO_DATE' is also documented on that page which you may need along with 'SUBDATE' which does date subtraction.

The first problem is to make sure or find a way to get your VARCHAR date into the MySQL Date format.
0
 

Author Comment

by:doc_jay
ID: 40527895
Thanks for your help on this.  I guess I'll have to put this question on the back burner until I sort out my varchar issue with 'pat_birthdate' column.  I'll ask again once its sorted.
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

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

9 Experts available now in Live!

Get 1:1 Help Now