0

I am building report in data warehouse which relay on comparing daily checks and payments for one of the restaurants. I need to be able to filter data on date field however it needs to be compared with string that looks like US date format but is string so

Select a.* from 
xx a, xy b
where 
a.payment_date = b.check_date

Format of a.payment_date is DD-MON-YY(date) and format of b.check_date is MM/DD/YYYY however it is a string. Any pointers to most efficient ways of solving this problem greatly appreciated.

5
  • Since you're asking about efficiency, doing JOIN with conversion operators will hurt performance in a bad way. The efficient way would be to add a real date column. Sadly, it's seldom the realistic one. Commented Feb 15, 2013 at 12:48
  • Where does this string actually come from? Commented Feb 15, 2013 at 12:51
  • It is old legacy system that spits out strings rather than dates. Commented Feb 15, 2013 at 12:53
  • @Lomo_effect A trigger that converts the inserted string to a new date column on insert/update could be a solution then. It would speed things up when doing JOIN for sure, but the work involved may not be worth the speed gain if the query isn't time sensitive. Commented Feb 15, 2013 at 13:23
  • @JoachimIsaksson what is better : converting string into date and comparing dates or converting date into string and comparing strings ?? I cannot add columns, build triggers or anything like this as I operate on quite large WH. Commented Feb 15, 2013 at 13:46

3 Answers 3

2

Convert check_date from string to date using TO_DATE():

SELECT a.*
FROM xx a, xy b
WHERE a.payment_date = TO_DATE(b.check_date, 'mm/dd/yyyy')

Or you can do the other way around, using TO_CHAR().

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, Joao it seems quite simple but yet proved to be very effective.
@Lomo_effect - To compare dates you should always apply TRUNC() in front of dates unless you are trying to compare date and time.
1

Convert both String dates into real dates (http://www.techonthenet.com/oracle/functions/to_date.php) so you can compare them date-wise.

Comments

0

i think this may work fr you

Select a.* from xx a, xy b where convert(datetime,a.payment_date) = convert(datetime,b.check_date)

1 Comment

oracle doesn't use convert. It uses to_date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.