Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime value into string

I am fetching the current date & time using NOW() in mysql. I want to convert the date value into a varchar and concat it with another string. How do I do it?

like image 998
Khilen Avatar asked Mar 06 '10 12:03

Khilen


People also ask

How do I convert datetime to string in Python?

To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.

How do I convert a date to a string in Excel?

Here are the steps to do this: Select all the cells that contain dates that you want to convert to text. Go to Data –> Data Tools –> Text to Column. This would instantly convert the dates into text format.


2 Answers

Use DATE_FORMAT()

SELECT   DATE_FORMAT(NOW(), '%d %m %Y') AS your_date; 
like image 147
Frank Heikens Avatar answered Oct 09 '22 00:10

Frank Heikens


This is super old, but I figured I'd add my 2c. DATE_FORMAT does indeed return a string, but I was looking for the CAST function, in the situation that I already had a datetime string in the database and needed to pattern match against it:

http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html

In this case, you'd use:

CAST(date_value AS char)

This answers a slightly different question, but the question title seems ambiguous enough that this might help someone searching.

like image 44
Chords Avatar answered Oct 08 '22 23:10

Chords