Migration of mysql dates in integer format - mysql

I want to migrate dates from integer format to DATETIME.
My dates in my old database have the following format:
olddb.table.date = 20131114 (INT)
olddb.table.time = 900 (INT) (9 AM, 24h clock)
new database:
newdb.table.datetime = 2013-11-14 9:00:00 (DATETIME)
How would I migrate this with purely SQL?

SELECT CAST(CONCAT(DATE(olddb.table.date),' ',TIME(olddb.table.time*100)) AS DATETIME);

You can convert your date part with STR_TO_DATE (MySQL documentation):
STR_TO_DATE(olddb.table.date, "%Y%m%d")
And for the time part, you can use the same function :
STR_TO_DATE(olddb.table.time, "%h%i")
Then, you can concatenate date and time parts and apply function to concatenation result :
STR_TO_DATE(concatenated_value, "%Y%m%d%h%i")
Where concatenated_value is built with :
CONCAT(olddb.table.date, olddb.table.time)

Try this:
cast(
concat(
table.date div 10000, '-',
lpad((table.date mod 10000) div 100, 2, '0'), '-',
lpad(table.date mod 100, 2, '0'), ' ',
table.time div 100, ':',
lpad(table.time mod 100, 2, '0'))
as datetime)
Here's a working example:
http://www.sqlfiddle.com/#!2/1ff75/1

Related

MySql search integer range from number with dash

I have table in that I have one field with dash value. Like...
I need to search this with between condition.
For example if I have one value 25 then I need to search the records which include the value 25 like 20-31. In above image there are 6 records which include 25 value. So it should return 6 records.
Please help me in this query ? What would be the query for that ?
You can use MySQL's substring_index() function to easily get the data before and after the dash:
select substring_index(yourcolumn,'-',1) as `lower`, substring_index(yourcolumn,'-',-1) as `upper`
from yourtable
This way you can return the records where a certain value falls between the range:
select * from yourtable
where 25 between substring_index(yourcolumn,'-',1) + 0 and substring_index(yourcolumn,'-',-1) + 0
The + 0 forces MySQL to convert the result of substring_index() to a numeric value before the comparison.
You can use the following solution using SUBSTRING_INDEX:
SELECT *
FROM table_name
WHERE 25 >= CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER)
AND 25 <= CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
-- or
SELECT *
FROM table_name
WHERE 25 BETWEEN CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER)
AND CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
demo: http://sqlfiddle.com/#!9/4ac7b3/3/0
I recommend you to change your table design. I would split the column using the VARCHAR datatype to two columns using the INTEGER datatype. You can add two new columns with the the following ALTER TABLE commands:
ALTER TABLE table_name ADD colNameA INT;
ALTER TABLE table_name ADD colNameB INT;
To split the values of you current column and update the values to the new columns you can use the following UPDATE command:
UPDATE table_name SET
colNameA = CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 1), '-', -1), UNSIGNED INTEGER),
colNameB = CONVERT(SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '-', 2), '-', -1), UNSIGNED INTEGER)
At the end you can remove the VARCHAR column using this ALTER TABLE command:
ALTER TABLE table_name DROP COLUMN col_name
Now you can use the following (simple) query to get the expected results:
SELECT *
FROM table_name
WHERE 25 >= colNameA AND 25 <= colNameB
-- or
SELECT *
FROM table_name
WHERE 25 BETWEEN colNameA AND colNameB
If you want to get values beween 35 and 39, you can use below query,
SELECT
*
FROM
yourtable
WHERE
35 && 39
BETWEEN SUBSTRING_INDEX(tablecolumn, '-', 1) + 0 AND
SUBSTRING_INDEX(tablecolumn, '-', - 1) + 0
I don't know how it possible with MySQL.
But using php it possible to check with range.
For e.g.
// First of all get all record from database.
$search = 10; // Your searching value.
// Loop all rows.
while($rows = mysqli_fetch_array($r)){
$explode = explode("-",$rows['dash']); // For get from-to value.
$range = isset($explode[0])&&isset($explode[1])?range($explode[0],($explode[1]-1)):array(); // For get range.
if(in_array($search,$range)){ // For check searching value is exist or not !
echo "Yes ! I get into ".$rows['dash']; // Do stuff.
}
}
Note: If 10-15 then it will check with 10,11,12,13,14.
According to me if you dont want to change the table structure then,
Just fetch the records as per your other condition, Then from that data check your amount between that field using foreach loop and explode. like
If you have $data as all data
foreach($data as $value){
$new_val=explode(',',$value['new_field']);
if(25 >= $new_val[0] && 25 <= $new_val[1]){
// here create new array
}
}

Mysql - Convert local date in UTC format

Good day,
I am having mysql dates stored in this format :
In my sql query I want to get these dates in UTC format.
I have tried CONVERT_TZ(dt,from_tz,to_tz)function but I cannot determine how can I get from_tz for the dates. I know that to_tz will be 'UTC' or '+0000'
Try this:
select convert_tz(`date`, replace(substring_index(`date`, ' ', -1), '00', ':00'), '+00:00')
Edit:
select convert_tz(`date`, concat(left(substring_index(`date`, ' ', -1), 3), ':', right(substring_index(`date`, ' ', -1), 2)), '+00:00')
Well, as mysql ignores TZ qualifiers in date, I suggest you do a semimanual TZ conversion
select
date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 +1000", -5, 3) hour),
date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 -1000", -5, 3) hour)
;
the result will be in UTC timezone as you expect

converting date and time without delimiter to date time with delimiter in sql server

I would like to convert date(without any delimiter ex. 31012015) and time(without any delimiter ex.0144) to date and time with delimiter. i.e.
31012015 => 31/01/2015 (dd/mm/yyyy)
0144 => 01:44 (hh:mm)
I tried various option in sql server (using various date-time format) but couldn't able to find out its solution.
try this
select replace(CONVERT(VARCHAR(10), GETDATE(), 103),'/','') as date_format,
left(replace(CONVERT(VARCHAR(10), GETDATE(), 108),':',''),4) timeformat
Since you only seem to care about the string representation of the value, you could use STUFF:
SELECT STUFF(#time, 3, 0, ':') as NewTime,
STUFF(STUFF(#date, 3, 0, '/'), 6, 0, '/') as NewDate
If you want your DATETIME without any delimiter, you can try this:
SELECT
replace(convert(varchar, getdate(),112),'/','') + replace(convert(varchar, getdate(),108),':','')

How to convert MSSQL to MySQL date in PHP

I am a PHP developer and I am working in converting MSSQL to MySQL file while converting data's
CAST(0x063A0B00 AS Date)
I cant convert this as a timestamp.
SELECT
CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(BinaryData),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND
AS DATETIME) AS converted_datetime
FROM
(
SELECT 0x0000987C00000000 AS BinaryData
UNION ALL
SELECT 0x00009E85013711EE AS BinaryData
) d
From how to cast the hexadecimal to varchar(datetime)?

CAST(0x00009F0900000000 AS DateTime) with Ruby or MySql

I'm trying to convert this string "0x00009F0900000000" into a date either through MySql or Rails as I'm working on a migration.
Can't find what format it is. Doesn't look like there's a way to convert hexadecimal value into a date through rails or mysql.
Solution found here: how to cast the hexadecimal to varchar(datetime)?
SELECT
CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(BinaryData),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND
AS DATETIME) AS converted_datetime
FROM
(
SELECT 0x00009F0900000000 AS BinaryData
) d
In ruby (and ignoring time)
require 'date'
str = "0x00009F0900000000"
p Date.new(1900,1,1) + str[0..9].hex
#<Date: 2011-06-21 ((2455734j,0s,0n),+0s,2299161j)>
This is the closest I found to your question:
> "0x00009F0900000000".to_i(16)
=> 174861003522048
> time = "0x00009F0900000000".to_i(16)
=> 174861003522048
> Time.at(time/1000)
=> 7511-02-16 05:58:42 +0100
> Time.at(time/1000000)
=> 1975-07-17 21:30:03 +0100
Try several divisors till you get what should be accurate