Solved

Help converting MSSQL query to MYSQL

Posted on 2014-12-19
4
251 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 83

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 83

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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