![]() |
MySQL / PHP gurus help with this code?
I have tried a couple help forums and #php but no luck so far.
I have a varchar field (date) with a date in MM-DD-YYYY format. I need to convert this to my (date2) field which is DATE in the YYYY-MM-DD format. Here is the sql statement I have so far - and it doesn't work: UPDATE b1datefix t1, b1datefix t2 SET t1.date2 = DATE_FORMAT(t1.date,'%Y-%m-%d') WHERE t1.id=t2.id; Any ideas? |
i dont work with mysql, but i'd think it would have some sort of globalization stuff built in so that it can natively accept the mm-dd-yyyy date format?
|
Quote:
|
If you were to do it in PHP check out the strtotime() function, get the timestamp, then use date() to get it back into the format you need. Not the cleanest but it would work.
|
You'll have to do it from the application.
The date_format and other functions expect a date in the default format %Y-%m-%d |
Quote:
|
Try something like this (I'm not awake yet so might have a typo in there heh)
UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date,FROM -7 FOR 2)); Enjoy :) |
Keith I think that would work, only problem is I am getting a syntax error and this is too advanced for me to troubleshoot:
#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 'FROM -7 FOR 2))' at line 1 |
Quote:
Try this: UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date FROM -7 FOR 2)); |
Quote:
he wrote that he wasn't fully awake ;) there is some garbage in the sql, (FROM and FOR).. the sql should read: UPDATE b1datefix SET date2 = CONCAT(SUBSTRING(date,-4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date, -7,2)); let us know how it goes ;) peace |
Quote:
|
Quote:
|
I ran the command and it affected all of the rows but it didn't pull in the date. All of the dates came in as: 0000-00-00
So something is wonky and I can't figure it out. |
Quote:
run that to see what comes out. |
Keith,
10 rows of -02- |
Quote:
I made a test case for oyu actually and on my server it works perfectly: mysql> desc datefix; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | date1 | varchar(10) | YES | | NULL | | | date2 | date | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> select * from datefix; +------------+-------+ | date1 | date2 | +------------+-------+ | 03-23-2007 | NULL | +------------+-------+ 1 row in set (0.00 sec) mysql> SELECT CONCAT(SUBSTRING(date1,-4),'-',SUBSTRING(date1,1,2),'-',SUBSTRING(date1 FROM -7 FOR 2)) from datefix; +-----------------------------------------------------------------------------------------+ | CONCAT(SUBSTRING(date1,-4),'-',SUBSTRING(date1,1,2),'-',SUBSTRING(date1 FROM -7 FOR 2)) | +-----------------------------------------------------------------------------------------+ | 2007-03-23 | +-----------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) |
Or avoid mysql's functions, and just get the data then update the data
PHP Code:
|
Quote:
However I think for Brad's purpose here doing it in mysql would be preferred instead of in the code...he could even use mysql's schedule feature to have it run every 5 mins or whatever he wishes, so no need to alter any code anywhere or even setup any cronjobs. |
MySQL 4.0.26-standard
That's probably why there is a problem. :/ |
Yah both ways work, would really depend on how often it needs to be done, if the source of the input could be switched/fixed.
|
Quote:
Otherwise, yeah you are probably stuck doing it on the php end. Last try that might work for you since the middle one did work form your output: CONCAT(SUBSTRING(date,7,4),'-',SUBSTRING(date,1,2),'-',SUBSTRING(date,4,2)) |
Quote:
Thanks guys I appreciate the help you just taught me a lot of new info. HUGSSSZZ!! |
Quote:
Guess this means I'm finally awake now and should head in to the office...later :pimp |
Quote:
I would if I had an epiphany like that. BTW - fun thread to watch... thanks geeks. :) |
All times are GMT -7. The time now is 08:39 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123