Solved

select * into (when old table has varchar instead of datetime)

Posted on 2013-12-03
5
295 Views
Last Modified: 2013-12-24
CREATE TABLE `a_messages` (
  `a_messages_id` int(11) NOT NULL auto_increment,
  `conversation_id` bigint(20) default NULL,
  `profile_id` varchar(20) default NULL,
  `sender` varchar(20) default NULL,
  `message_id` bigint(20) default NULL,
  `message_text` varchar(1000) default NULL,
  `dateAgo` datetime default NULL,
  `message_read` tinyint(4) default NULL,
  `this_user` varchar(20) default NULL,
  PRIMARY KEY  (`a_messages_id`),
  UNIQUE KEY `unique_message_id` (`message_id`)
) 

CREATE TABLE `a_messages2` (
  `a_messages_id` int(11) NOT NULL auto_increment,
  `conversation_id` bigint(20) default NULL,
  `profile_id` varchar(20) default NULL,
  `sender` varchar(20) default NULL,
  `message_id` bigint(20) default NULL,
  `message_text` varchar(1000) default NULL,
  `dateAgo` varchar(20) default NULL,
  `message_read` tinyint(4) default NULL,
  `this_user` varchar(20) default NULL,
  PRIMARY KEY  (`a_messages_id`),
  UNIQUE KEY `unique_message_id` (`message_id`)
)

Open in new window




I am copying old values from a_messages2 (a backup table) into a_messages

The problem is I want either a date in this format

2013-12-04 00:44:52
2013-12-04 00:45:53
2013-12-04 00:46:58

or null

because
a_messages.dateAgo is datetime but
a_messages2.dateAgo is varchar and had text other than a date in this format sometimes

For example:
a_messages2.dateAgo:
2013-12-04 00:44:52
2013-12-04 00:45:53
2013-12-04 00:46:58
2013/12/04
2013
Hello

expected output :
a_messages2.dateAgo:
2013-12-04 00:44:52
2013-12-04 00:45:53
2013-12-04 00:46:58
NULL
NULL
NULL


if date in correct format, copy date
else
put null in the column




I do not want to create many different tables creating null results just so I can copy

I would prefer to do this in one
select * into statement
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 39695052
Hi!

You could do this something like this

insert into a_messages
select
...
case when dateAgo REGEXP 'somedatepattern' then  DATE(dateAgo) else NULL end as DateAgo,
....
from a_messages2

You can always test the outcome by executing only the select and ones you are satisfied then add the insert statement
Hope this helps,

Regards,
   Tomas Helgi
0
 

Author Comment

by:rgb192
ID: 39697376
can mysql do a regular expression for date or do I need to use php?
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 39697591
Hi!

You can use regular expressions in sql queries and delete/update statements where clauses. In this case you are using the pattern matching against the string representation of a date value.

http://dev.mysql.com/doc/refman/5.5/en/regexp.html
http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/
http://www.regular-expressions.info/mysql.html
http://www.tutorialspoint.com/mysql/mysql-regexps.htm

Regards,
    Tomas Helgi
0
 

Author Comment

by:rgb192
ID: 39703210
I still do not understand that portion of the question, so I opened another question about regex

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28312994.html
0
 

Author Closing Comment

by:rgb192
ID: 39738193
your case statement and

SELECT <column_name>
FROM <table_name>
WHERE <column_name>
RLIKE  '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$';

Will work together for a good mysql only solution

The solution I will be using is with php and strotime that Ray is working on.

Thanks
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question