Solved

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

Posted on 2013-12-03
5
291 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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