Question

I'm using MAMP's phpMyAdmin as my database. I'm having troubles using the CONVERT sql function. Is this a common problem?

SELECT CONVERT(varchar(10), Date, 103)

This will give me the following syntax error. Which is one of the usual errors given but I've been trying a lot of different ways.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(10), Date, 103)' at line 1

If the above can't be solved can I bypass this issue by adding a new column with a specific date format style?

Was it helpful?

Solution 2

The date is already in the correct format but it is stored as varchar(10) Current looks like: 21/04/2014

Yes, I have a column that has dates but is set to varchar as it wasn't being used now I need to convert these into dates.

If you are trying to get a date form from a string form of date, then

select str_to_date( date_column, '%d/%m/%Y' ) as date from table_name

This will return 21/04/2014 as 2014-04-21, regular default date format in MySQL.

OTHER TIPS

You are trying to use syntax in database.

does not have 3rd parameter for CONVERT function.

CONVERT(expr,type), CONVERT(expr USING transcoding_name)

The CONVERT() and CAST() functions take an expression of any type and produce a result value of a specified type.

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

to do the get same format in you need to use DATE_FORMAT() function

this should give you the same resultDATE_FORMAT(date,'%d/%m/%Y')

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

 select DATE_FORMAT(Current_Date(),'%d/%m/%Y')

here is SQL Fiddle http://sqlfiddle.com/#!2/d41d8/36014

It depends on what you want to do.

If you're trying to convert an integer that represents a Unix timestamp to a date with MySQL, you may want to use FROM_UNIXTIME()

See also: MySQL: Convert INT to DATETIME

If you're trying to convert a string that represents a date into a date, you can use STR_TO_DATE()

After that, if you perform some processing on the date and want to convert it back to a string, you'll need DATE_FORMAT()

See also: mySQL convert varchar to date

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top