Home » SQL & PL/SQL » SQL & PL/SQL » Convert date format for easier lookup
Convert date format for easier lookup [message #606254] Tue, 21 January 2014 13:04 Go to next message
tdnguyen
Messages: 2
Registered: January 2014
Location: Arlington
Junior Member
Hello,

I'm working on a SQL ORacle query. I have a column called Date_IN that contains string values of dates formatted like DD-MM-YY (20-May-13). How can I filter data by just looking at year only. For example, like where year(date_in) >= 2013. How can I convert something like that or by month and year only?

Also, I have a query to retrieve data for the latest weekending. For example, Where Date_in = to_date('01/12/2014','MM/DD/YYYY') . This week I have to change the code to
Where Date_in = to_date('01/17/2014','MM/DD/YYYY'). Is there a way to have this automatically updated to the latest week ending when it is available.

Thanks




Re: Convert date format for easier lookup [message #606256 is a reply to message #606254] Tue, 21 January 2014 13:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I don't clearly understand what you want but what is sure is the first thing you have to do is to convert your column to DATE datatype.

Re: Convert date format for easier lookup [message #606258 is a reply to message #606256] Tue, 21 January 2014 13:14 Go to previous messageGo to next message
tdnguyen
Messages: 2
Registered: January 2014
Location: Arlington
Junior Member
how to convert the column to Date datatype
Re: Convert date format for easier lookup [message #606259 is a reply to message #606254] Tue, 21 January 2014 13:17 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
You created yourself an unneccessary problem just by storing dates as string values - bad design. Now you have to deal with it. Now you have to use something like:

TO_DATE(date_in,'DD-MM-YY') >= DATE '2013-01-01'


which means it will not use index on date_in, even if you have one.

SY.
Re: Convert date format for easier lookup [message #606260 is a reply to message #606258] Tue, 21 January 2014 13:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Create a new column with DATE datatype, fill it with the current data using TO_DATE, drop the old column, rename the new column.

Re: Convert date format for easier lookup [message #606854 is a reply to message #606254] Thu, 30 January 2014 01:23 Go to previous message
subhra_88
Messages: 14
Registered: April 2007
Location: Bangalore
Junior Member
Quote:
Is there a way to have this automatically updated to the latest week ending when it is available.


First thing is you need to change the column to date datatype as others have advised. After that you can easily use the 'sysdate' to get the date which is 7 days back or manipulate as per your needs using date functions that oracle provides.
Previous Topic: How to get back purged data into the tables
Next Topic: PLS-00201: identifier 'EMPNO' must be declare
Goto Forum:
  


Current Time: Thu May 09 02:31:06 CDT 2024