Solved

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

Posted on 2013-12-03
5
288 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
  • 3
  • 2
5 Comments
 
LVL 24

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 24

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now