Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert str to date and then compare

Posted on 2008-06-09
3
Medium Priority
?
1,606 Views
Last Modified: 2012-06-22
I need to compare 2 date fields; one is a string, the other is a datetime.  The string is in the yyyymmdd format.  How do I convert these two data types, and then compare them?  I will want to display data where the data associated with the string is more recent than the datetime.
0
Comment
Question by:AMS_RN
  • 2
3 Comments
 
LVL 10

Expert Comment

by:MaduKp
ID: 21749126
Could you please give more details?
what is the database?
what is the programming language you are using?
0
 

Author Comment

by:AMS_RN
ID: 21750977
Sorry about that;  I'm using Crystal Reports XI, and the database is in sql (it's a healthcare database.)  I am comparing the date that a medication was stopped (which is a string) to the date of the office visit (which is a datetime).  I only want to display medications that have a stop date AFTER the office visit date (so the medication is still current when the patient leaves the clinic).
0
 
LVL 10

Accepted Solution

by:
MaduKp earned 375 total points
ID: 21757590
Ok ,
Please see below SOL statement, you can use this as a SP and use,
Use <Your DB - healthcare database>

========================================================================

DECLARE  @medication_stopped datetime
SET @medication_stopped = '2008-06-11'  send your date here

To compare two dates is done as below ,Here the table_2 is <whatever your table have the filed date of the office visit> 

select * from table_2
where convert(varchar(8), date_of_the_office_visit)
< convert(varchar(8), @medication_stopped)

=========================================================================

Suppose your table is something like this,
ID            date_of_the_office_visit
1      06/10/2008 12:00:00 AM
2      06/11/2008 12:00:00 AM
3      06/07/2008 12:00:00 AM
4      06/19/2008 12:00:00 AM
5      09/10/2008 12:00:00 AM
6      02/10/2008 12:00:00 AM

The output will be
1      2008-06-10 00:00:00.000
3      2008-06-07 00:00:00.000
6      2008-02-10 00:00:00.000
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you are like me and like multiple layers of protection, read on!
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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