?
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 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