mysql datetime update to one column - mysql

I have made a really silly mistake of having a seperate date and time field in my DB. I really need to put these together and just have one field (datetime) Luckily these are individually in the corect format. Is there a way I can di an SQL statement to take the date, time (with a space inbetween) and input these in to the new datetime field? I have around 3000 records in this table so really dont want to do it by hand.
Thanks
Richard

run:
UPDATE dates SET datettime_field = CONCAT(date_field,' ', time_field);

You can use CONCAT_WS to do a string concatenation:
UPDATE table
SET newfield = CONCAT_WS(' ', oldfield1, oldfield2);

Another solution is:
update table set datetime_field = addtime(date_field, time_field);

Related

Convert All Values in a Column From VARCHAR to DATE in MySQL

I know how using STR_TO_DATE(str,fmt); will change the format of a specific string, but if I have a column of all different dates in a text format (ex. 1/11/2016, 1/25/2016, 6/27/2015...) how do I convert all of the data in the column to DATE format? From everything I'm reading, it seems you need to provide the exact value to be converted each time.
You can use multiple ifnull fuction for this.
select ifnull(STR_TO_DATE(a,'%Y-%m-%d'),sTR_TO_DATE(a,'%Y-%d-%m')) from [YourTable];
update [YourTable] set timestamp=ifnull(STR_TO_DATE([Colum],'%Y-%m-%d'),sTR_TO_DATE(a,'%Y-%d-%m')) ;
Hope this helps.
Thanks Mark B.
str_to_date(yourfield, '%d/%m/%Y'), done. you can use fields in a record as part of the update query, e.g. update foo set bar=bar+1 is completely legitimate/valid.
So I used:
update [My_Table]
set Create_Date = str_to_date(Create_Date, '%d/%m/%Y')

Update time on timestamp column

I'm trying to select with reference to one column changing its time in MySQL, but I don't know how.
How will I do it?
Example:
Time original: 2015-07-20 22:10:52
Updated: 2015-07-20 23:59:59
You can use timestamp to join the current timestamp's date with the time you want to set:
UPDATE mytable
SET mytimestamp = TIMESTAMP(DATE(mytimestamp), '23:59:59')
To update with no reference to previous column value, it's no different from other columns. To update with it, and based on certain part (second, month, year, whatever), you can use DATE_ADD or DATE_SUB function. Any other function in the same page might be useful, too, depending on your needs.
It doesn't matter what type of column you have, your table can be modified with a standard UPDATE statement.
You can also use the some special time/date functions to help you get the right format.
UPDATE mytable SET mytimestamp = mytimestamp::date + '23:59:59'::time;
it should work.

Deleting all rows which contain a unique value

I have a database and my dataset is really messed up. The column of importance is the a "uniqueidentity" number where some records have "&&" or "%%" in contained at the end of the value. If it does, I would like to delete the entire row from the table. uniqueidentity = VARCHAR
Does anybody have any ideas on how to do this using a SQL Query?
Thanks in advance
you could use
DELETE FROM table WHERE RIGHT(uniqueidentity, 2) = "&&" OR RIGHT(uniqueidentity, 2) = "%%"
know thy sql commands young one and all troubles will begone
sql delete result from query
http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
SQL - how to select words with certain values at the end of word
Try like this:
DELETE FROM tableName WHERE
uniqueidentity REGEXP '%%$' OR
uniqueidentity REGEXP '&&$';
I have created supporting SQL FIDDLE with select query which you can change it to delete as above.

How to do MySQL + Substring? + replace?

I am not very good with SQL and would like to be able to become better.
I am having some trouble trying to preform a certain table manipulation.
I would like to be able to select the substring out of the ProgUID column below
something like...
SUBSTRING(table.ProgUID,3,12);
which would give me CAMVE-9701 for the ProgUID P-CAMVE-9701-1 (removing the P- from the beginning and the -1 from the end), and then insert the substring into that rows UID.
I assume this should be fairly easy, and I have been trying to figure it out but havent had much luck.
If there is a better approach please let me know!
Thanks in advance for your thoughts / help!
use UPDATE statement
UPDATE tableName
SET UID = SUBSTRING(ProgUID,3,12)
See SQLFiddle Demo
If the portion you want is always 12 characters, then
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, 12)
otherwise
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, LENGTH(ProgUID)-2)

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).