Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Old 10-14-2011, 01:30 PM Change Dates
Red_X_'s Avatar
Extreme Talker

Posts: 158
Location: Houston
Trades: 0
I have a SQL database that stores dates as CURDATE() (2011-10-10) for example. I want to change the format of all the current records to (mm/dd/yyyy) how do I go about doing this?

Thanks a bunch,
X
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
 
Register now for full access!
Old 10-14-2011, 03:09 PM Re: Change Dates
chrishirst's Avatar
Defies a Status

Posts: 44,046
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
What family of SQL server?

try

Code:
 FORMAT(column, 'MM-DD-YYYY')
should work on most
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-14-2011, 03:47 PM Re: Change Dates
Red_X_'s Avatar
Extreme Talker

Posts: 158
Location: Houston
Trades: 0
Quote:
Originally Posted by chrishirst View Post
What family of SQL server?

try

Code:
 FORMAT(column, 'MM-DD-YYYY')
should work on most
MySQL.

This is what I ended up doing (i'll defiantly log that though, as its much easier). Change the field datatype from DATE to varchar(11)

PHP Code:
<?php
    
function formatdate$s )
    {
    return 
date"m/d/Y"strtotime$s ) );
    }

        
$query 'SELECT id, date 
            FROM  `TBL` 
            WHERE DATE = DATE_FORMAT( DATE,  "%Y-%m-%d" )'
;
        
$result mysql_query($query) or die(mysql_error());
        while(
$data mysql_fetch_object($result)) {
            
$newdate formatdate($data->date);
            
$query 'UPDATE TBL SET date = "'.$newdate.'" WHERE id = '.$data->id;
            
$date mysql_query($query) or die(mysql_error());
        }
        echo 
'Updated Records';
    
?>
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 10-14-2011, 04:34 PM Re: Change Dates
chrishirst's Avatar
Defies a Status

Posts: 44,046
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
definitely not defiantly

One thing you should never, ever do is store dates as a string type, because one day it will come back on you.
that's definitely a programmer/wannabe DBA shortcut which rarely ends well.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-14-2011, 05:38 PM Re: Change Dates
Physicsguy's Avatar
404 - Title not found

Posts: 1,066
Name: Scott Kaye
Location: Ontario
Trades: 0
Just gonna hop into this thread and reply to what Chris said above me ^. I always store my dates in an INT field (I store the Epoch timestamp), and the reason for that is entirely that I don't know exactly how the date field works in MySQL. Are there special limitations? The timestamp (PHP's time() function) works great for me, because you can format it however you please after it has been entered in the DB.
__________________

Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile Visit Physicsguy's homepage!
 
Old 10-14-2011, 06:35 PM Re: Change Dates
Red_X_'s Avatar
Extreme Talker

Posts: 158
Location: Houston
Trades: 0
Quote:
Originally Posted by chrishirst View Post
definitely not defiantly

One thing you should never, ever do is store dates as a string type, because one day it will come back on you.
that's definitely a programmer/wannabe DBA shortcut which rarely ends well.
Mind explaining as to why? Knowledge is power, share?
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 10-14-2011, 06:59 PM Re: Change Dates
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
Just gonna hop into this thread and reply to what Chris said above me ^. I always store my dates in an INT field (I store the Epoch timestamp), and the reason for that is entirely that I don't know exactly how the date field works in MySQL. Are there special limitations? The timestamp (PHP's time() function) works great for me, because you can format it however you please after it has been entered in the DB.
A datetime filed (at least in MySQL) is formatted as "YYYY-MM-DD HH:MM:SS". Using this format will allow the SQL engine to fully maximize date and time functions in a query.

To allow PHP to handle the datetime stamp, just use the strtotime() built-in function to convert it into a unix timestamp integer.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 10-14-2011, 07:02 PM Re: Change Dates
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Because one day you may want to use the field in some expression like sorting/ordering according to that date or add/subtract it to some other date, and you won't be able to (at least not correctly) because those operations doesn't work for strings.

It's better to use the existing date types and then format them the way you want them, either with i.e. FORMAT as chris suggested, or after the DB query response with whatever language you are using.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-14-2011, 07:43 PM Re: Change Dates
Physicsguy's Avatar
404 - Title not found

Posts: 1,066
Name: Scott Kaye
Location: Ontario
Trades: 0
Ahh, very good reasons, you guys!! I've always wondered the real use for strtotime, I thought it was for processing parsed dates back into their timestamps, but at the time I didn't know that was possible since some elements of the date could be missing (example: 'Friday, January, 2012' is missing the day, hour, minute, second, etc).

Thanks!
__________________

Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile Visit Physicsguy's homepage!
 
Old 10-15-2011, 05:17 AM Re: Change Dates
chrishirst's Avatar
Defies a Status

Posts: 44,046
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I wonder what the mad rush is going to be like when the world realises that all MySQL TIMESTAMP fields are going to fail?

reason:
Timestamp fields are stored as four bytes (32bits) and is the number of milliseconds since the "Unix Epoch" 00:00:00 01/01/1970 and the field will actually run out of space in 2037.

Will we be seeing the headlines about the "Twenty ThirtySeven" bug?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-15-2011, 09:48 AM Re: Change Dates
Physicsguy's Avatar
404 - Title not found

Posts: 1,066
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Will we be seeing the headlines about the "Twenty ThirtySeven" bug?
Naw, programmers can just change their DATE fields to varchar(255) (255 so it lasts for a VERY long time)
__________________

Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile Visit Physicsguy's homepage!
 
Old 10-15-2011, 10:25 AM Re: Change Dates
NullPointer's Avatar
Will Code for Food

Posts: 2,883
Name: Matt
Location: Irvine, CA
Trades: 0
No code being written today will still be in use in 2037... right?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-15-2011, 11:17 AM Re: Change Dates
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Will we be seeing the headlines about the "Twenty ThirtySeven" bug?
Surely we will. People will be stockpiling canned food to last a life time or two and hide in their nuclear shelters
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Change Dates
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55436 seconds with 11 queries