Solved

Convert date to text

Posted on 2013-01-06
2
233 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

12 Experts available now in Live!

Get 1:1 Help Now