Current location: Hot Scripts Forums » Other Discussions » Database » copying varchar to datetime


copying varchar to datetime

Reply
  #1 (permalink)  
Old 05-26-09, 10:42 PM
badmullah badmullah is offline
New Member
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Spin copying varchar to datetime

using sql server 2005

I have a column (A) that has been defined as varchar and has date values in it
by someone who did not know that it can have a datetime attribute instead.
I am trying to fetch values such as 3/4/2004 or 3.4.2004 or Null from thsi varchar column
and move them into another column (B) that has datatime attribute.

can someone type the commands that I need to execute to upate column B from column A please?

Thanks
Reply With Quote
  #2 (permalink)  
Old 05-27-09, 07:28 AM
protocode protocode is offline
Newbie Coder
 
Join Date: May 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
DATETIME stores in this format YYYY-MM-DD H:i:s. Here is a quick set of date formatting code you may find useful. I assume your dates list the month first (MM/DD/YYYY) and not the day (DD/MM/YYYY). If they do list the day first, it is no problem, just a simple switch on the return. Also, this allows you to define the separating character i.e. mm / dd / yyyy or mm . dd . yyyy. For example $var = dateNormtoUnix("/", $date); or $var = dateNormtoUnix(".", $date);

This is for a date formatted with the month first MM/DD/YYYY:
PHP Code:

    function dateNormtoUnix($dformat$beginDate)

{
        
$date_parts1=explode($dformat$beginDate);
        
    return 
date("Y-m-d",mktime(0,0,0,$date_parts1[0],$date_parts1[1],$date_parts1[2]));

This is for a date formatted with the day first DD/MM/YYYY:
PHP Code:

    function dateNormtoUnix($dformat$beginDate)

{
        
$date_parts1=explode($dformat$beginDate);
        
    return 
date("Y-m-d",mktime(0,0,0,$date_parts1[1],$date_parts1[0],$date_parts1[2]));

You query each date then, clean them up and store them in the correct field.
Reply With Quote
Reply

Bookmarks

Tags
datetime


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert into datetime MySQL format. Oskare100 Database 2 01-23-07 11:23 AM
datetime data type problem... Clark_Kent Database 2 10-07-06 11:16 AM
Help anyone ? phpfreek PHP 6 09-26-06 06:13 AM
Default value for the Datetime data type... Clark_Kent Database 2 08-09-06 07:40 AM
Converting mySQL datetime into something readable (with PHP) bitesize PHP 0 10-27-03 01:22 AM


All times are GMT -5. The time now is 05:07 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.