Solved

Convert str to date and then compare

Posted on 2008-06-09
3
1,598 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
Comment Utility
Could you please give more details?
what is the database?
what is the programming language you are using?
0
 

Author Comment

by:AMS_RN
Comment Utility
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 125 total points
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
A list of useful business intelligence software.
This video demonstrates basic masking and how to edit the mask to reveal the desired image.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.

743 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

18 Experts available now in Live!

Get 1:1 Help Now