[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-12-03
5
Medium Priority
?
298 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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