Solved

Convert date to text

Posted on 2013-01-06
2
236 Views
Last Modified: 2013-01-28
A bit of a strange one, but I want to search a date field as a text

eg:-
*2002* which would look for 2002/1/1 - 2002/12/31

I have searched for a way to convert to text, such as text('date'). I don't want to convert the field to a text, as I still want to order by the field.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 9

Assisted Solution

by:sognoct
sognoct earned 250 total points
ID: 38749164
select *
from tblTable 
where DATE_FORMAT(yourDate, '%Y /%m/%d') like '%2012%' 
order by yourDate

Open in new window


but consider also the idea to use datetime in query

SELECT * 
FROM  tblTable 
WHERE YEAR( mydate ) =2013
order by yourDate

Open in new window

0
 
LVL 24

Accepted Solution

by:
johanntagle earned 250 total points
ID: 38749428
If possible depending on the types of expected search strings I would recommend converting the search string to a date range.  For example  in your application code if it detects that the search string as a year then in your SQL do a:

WHERE date_column between '2012-01-01' and '2012-12-31'

(or '2012-12-31 23:59:59', if date_column is actually a datetime with time defined)

The problem with doing date_format or year or any other function on the date_column is that it will always result in a full table scan, even if you have an index defined for date_column.  Big no-no if the table is of considerable size.

I wouldn't advise doing a LIKE '%2012%' on a string column either, whether you convert the existing or create another column -- that's another performance killing full table scan.  At least take a look at MySQL FULL-TEXT search (see dev.mysql.com/doc/refman/5.1/en/fulltext-search.html and devzone.zend.com/26/using-mysql-full-text-searching/)
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

17 Experts available now in Live!

Get 1:1 Help Now