Trouble Converting varchar to date - mysql

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?

You are trying to use sql-server syntax in mysql database.
mysql 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 mysql 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

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.

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

Related

How can I convert a varchar into a date in MySQL Workbench?

Probably it's super simple but i've been stuck some hours on this.
I have a column called "Publish_Date" which is a varchar, but my date shows like this: 17.01.11 (year.day.month) and I want to convert it to a date (at this point, any date format it's ok).
Every time i tried to use "convert" or "cast" it gives me a syntax error or the data doesn't change or all the data in the column changes to "null" values.
I'd appreciate if you can help me.
Assuming your data is all greater than 2000 then you can add missing part of YEAR then cast it.
SELECT CAST(CONCAT('20', Publish_Date) AS DATETIME);
You can use STR_TO_DATE with the format %y.%m.%d since this is how your date value is stored
select
str_to_date(birth_date, '%y.%m.%d')
from
mytable
Here is an SQL Fiddle I created for this case

SQL statement error in VB.Net

This is the SQL statement which I taken from debug mode in VB.Net.
SELECT dt_Date as 'Date',
s_Time as 'Time',
s_For as 'For',
s_Categ as 'Category',
s_Count as 'Count',
s_Remarks as 'Remarks'
FROM Entry
WHERE (s_ENo = '22' and dt_date BETWEEN '06-05-16' And '27-05-16')
I am not sure what's wrong with the above statement since everything seems to be fine to my eyes.
Description of the Error Message
Additional information: Conversion failed when converting date and/or
time from character string.
Any suggestions how to solve this riddle?
Instead if string literals, use strongly-typed parameters. In addition to avoiding data type conversion issues on the SQL Server side, parameters:
Avoid the need to format date/time string literals in a particular way
Improve performance by providing execution plan resue
Prevent SQL injection (barring dynamic SQL on the server side)
If you are using MS-SQL server than use following instructions.
The basic solution is that you have to provide date into either mm/DD/YYYY format or in YYYY-MM-DD date format into ms-sql query.
So, before passing date to query convert your date into either mm/DD/YYYY format or in YYYY-MM-DD format.
You have to format your date string to YYYYMMDD or if you know the server date format then format same as server

Why i got null from this query

Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');

mysql date_format unable to parse 20-may-2013

using date_format as specified in the doc from mysql
select date_format('20-May-2013', '%d-%b-%Y');
returns null, why?
You are trying to parse a date using DATE_TIME(), but that function is meant to do the inverse - take a known formatted date and display it using a custom format. What you probably want is the STR_TO_DATE() function:
select str_to_date('20-May-2013', '%d-%b-%Y');

MySQL - How to parse a string value to DATETIME format inside an INSERT statement?

I have a MySQL database, with a column that is date type DATETIME.
I am getting a string value for a date / time from an external application. That string value looks like this:
'5/15/2012 8:06:26 AM'
MySQL throws an error on the INSERT: "Error. Incorrect datetime value". My workaround was to change the column type to VARCHAR, which works, but I really need the data as a proper Date & Time for future use.
I researched accepted formatting for MySQL DATETIME values, and found that MySQL wants the DATETIME format as 'YYYY-MM-DD HH:MM:SS'.
I can't change the external application to reformat the date / time string in a format, so my only chance is to deal with it.
What I need to do, I think, is parse the existing string, using MySQL syntax, inside of my INSERT statement, but I'm not sure how to do that. I have some idea that I need to use a SELECT clause, perhaps STR_TO_DATE, somehow in combination with my INSERT statement.
Here is my current INSERT statement. I removed the other fields that are not causing a problem, just to make the example clean.
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES ('5/15/2012 8:06:26')
Use MySQL's STR_TO_DATE() function to parse the string that you're attempting to insert:
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES
(STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r'))