Wrong MYSQL (date) output - mysql

i have a database that contains over 2000 records and the date is wrongly formatted (mm/dd/yyy). I need to change this with mysql into dd/mm/yyy.
i have this code:
UPDATE wp_team_workshop_availbility SET available_date = DATE_FORMAT('available_date', '%d-%c-%y')
but all i creates is an empty field.

If you are storing it as a date (datatype), it is just a date - there is no format. However, you also don't want available_date as a quoted string, which is trying to convert the string "available_date" into a date.
My guess is you have the date stored as a string (you really shouldn't). However, what you will want is something more like:
UPDATE wp_team_workshop_availbility
SET available_date = DATE_FORMAT(STR_TO_DATE(available_date,'%c/%d/%Y'), '%d-%c-%y');
i.e. you need to convert the string to a date and then convert it back to a string.
But really, you should take advantage of this opportunity to change your storage so you are using the right datatype.

Related

Update-Set for a Date type not processing query part correctly in MS Access 2007

I'm trying to extract a text date into a date type field and change the format from yyyymmdd to ddmmyyyy in the process. I have set up a simple select statement checking that the dates are valid and if not setting a default date and this worked fine, no bad dates.
SELECT
IIf(isdate(Format(Left([EffectiveDate],10),"dd/mm/yyyy")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#) AS Expr1
FROM Relationships;
But when I embed this exact same select statement in an Update Set Query:
UPDATE Relationships
SET MSDate = IIf(isdate(Format(Left([EffectiveDate],10),"dd/mm/yyyy")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#);
the dates are formed as mmddyyyy and not ddmmyyyy as the select query does.
Interestingly, when I tried to change the format type to "long date"
UPDATE Relationships
SET MSDate = IIf(isdate(Format(Left([EffectiveDate],10),"long date")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#);
I got the default 01/01/1900 result suggesting what was extracted was not a valid date. By the way, just using the query in a Select statement worked just fine.
I can't help thinking that something is happening in the conversion to date type. I even tried to do DateValue on the query but still no joy.
Since you're using Left([EffectiveDate],10), I assume that the text field actually contains yyyy-mm-dd (the ISO format).
You should leave the string in this format (Access understands ISO and US format mm/dd/yyyy best), and convert it with the CDate() function.
UPDATE Relationships
SET MSDate = IIf(IsDate(Left([EffectiveDate],10)),
CDate(Left([EffectiveDate],10)),
#1900-01-01#);

Change Date Format in SQL Server 2008

I have a table where users used to enter random date formats like
dd/MM/Year or MM/dd/Year.
But I want to update all those dates to this format Year/MM/dd
I have made a cursor and changed data by CHAR INDEX and sub string..
but is there any easy way to change the date format to the one i want ?
and also how can I make my default database date format = Year/mm/dd
only database not server?
You can convert it into different styles.
for example:
select convert(varchar(15),getdate(),103)
will give today's date in dd/mm/yyyy.
Full list of converting styles supported by MSSQL is here

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

UNIX_TIMESTAMP outputting NULL in MySQL?

I've got a table setup which has populated data. In column "date", I have dates in the following format:
yyyymmdd i.e. 20131110
I have created a new field and called it newdate with the text format.
Then, I open up the SQL window and put the following in
UPDATE wl_daily
SET
newdate = UNIX_TIMESTAMP(date)
For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason
Any suggestions?
That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.
Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.
While John Cronde's answer is correct - it doesnt help your situtation
UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))
will do the conversion for example
SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d'))
returns
unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))
---------------------------------------------------
1384128000
You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production

MySqlImport - Import a date field not in the proper format

I have a csv file that has a date field in a format like (among other fields):
17DEC2009
When I do a mysqlimport, the other fields are imported properly, but this field remains 0000-00-00 00:00:00
How can I import this date properly? Do I have to run a sed/awk command on the file first to put it into a proper format? If so, what would that be like? Does the fact that the month is spelled out instead of a number matter?
STR_TO_DATE() enables you to convert a string to a proper DATE within the query. It expects the date string, and a format string.
Check the examples in the manual entry to figure out the correct format.
I think it should be along the lines of %d%b%Y (However the %b is supposed to produce Strings like Dec instead of DEC so you will have to try out whether it works).
I had this issue in the past. What I had to do was to utilize LOAD DATA and set the appropriate expression here -
[SET col_name = expr,...]
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Here is the approach I took to solve similar problem. My use case was bit complex with so many columns, but making here simple to present the solution.
I have Persons table with (Id int autogen, name varchar(100),DOB date), and few million of data(name,DOB) needs to be populated from CSV file.
Created additional column in persons table with name like (varchar_DOB varchar(25)).
Imported data using mysqlimport utility into columns(name,varchar_DOB).
Executed update query that updated DOB column using str_to_date(varchar_DOB,'format') function.
Now, I have expected data populated DOB column.
The same logic could be applied in doing even other kind of data formatting like double,time_stamp etc.