Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help converting MSSQL query to MYSQL

Posted on 2014-12-19
4
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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