Datetime field overflow error in mysql - mysql

I am trying to insert into a mysql table after conversion from mssql. The datetime format was in hexadecimal, Then i wrote a function for converting it to datetime. When i try to insert the values, I am getting datetime field overflow error.
My insert statement looks like this:
INSERT into BankMaster (BankCode, BankName, CREATEDID, CREATEDDATETIME, UPDATEDID, UPDATEDDATETIME) VALUES ('N','xxxx','admin',cast(ConvertSQLServerDate(0x98A90076) as datetime), NULL, NULL);
Function is as follows:
create function ConvertSQLServerDate(dttm binary(16))
returns datetime
return CAST(
'1900-01-01 00:00:00' +
INTERVAL CAST(CONV(substr(HEX(dttm),1,8), 16, 10) AS SIGNED) DAY +
INTERVAL CAST(CONV(substr(HEX(dttm),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND AS DATETIME);
Any help would be highly appreciated.

I had generated script with smalldatetime as data type in MSSQL. So I was getting an 8bit hexadecimal number. Later on, I converted into datetime datatype which resulted in 16bit hexadecimal value and I got my desired result.

Related

How to convert a DATETIME to unix timestamp (and handle negative dates correctly)?

Up until now I've been using this function
CREATE FUNCTION dt2ts (d DATETIME)
RETURNS BIGINT deterministic
RETURN TIMESTAMPDIFF(SECOND, FROM_UNIXTIME(0), d);
But as I just discovered this gives incorrect results if your date does not have the same offset as 1970-01-01, i.e. it's incorrect if your date is in daylight savings time.
UNIX_TIMESTAMP() doesn't work because it can't handle dates prior to the unix epoch.
So how can I get the correct unix timestamp for any given datetime?
Figured out a solution.
CREATE FUNCTION dt2ts (d DATETIME)
RETURNS BIGINT deterministic
RETURN TIMESTAMPDIFF(SECOND, '1970-01-01', convert_tz(d,##session.time_zone,'UTC'));
You need to import timezones in order for this to work. I don't think there's a way to do this without.

How to convert a date field to a date time format? SQL 2008

The conversion of a date data type to a datetime data type resulted in an
out-of-range value.
The statement has been terminated.
I just need to convert a date field to a date time format.
Example of what the code would look like to convert from date to datetime2
DECLARE #d1 date;
SET #d1 = GETDATE()
-- When converting date to datetime the minutes portion becomes zero.
SELECT CAST (#d1 AS datetime2) AS [date as datetime2]
For more information about cast and convert, see the Microsoft reference.
You probably get out of range exception because Date supports a date range between
January 1, 1 and December 31, 9999 , while DateTime supports a date range only between
January 1, 1753 to December 31, 9999.
Want to know Why 1753? Read this. (Recommended reading for anyone that likes trivia items)
Try converting to Datetime2 instead of Datetime, this should be OK since Datetime2 supports a date range similar to date.
the conversion can be done simply by using CAST:
SELECT CAST(YourDateTypeColumn As Datetime2)
FROM YourTable

Sql Cast hexadecimal date to mysql varchar(date)

I have code for hexadecimal CAST(0x0000A2F5016C1769 AS DateTime) to mysql datetime,
from how to cast the hexadecimal to varchar(datetime)?
code:
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 0x0000A34900BD693D AS BinaryData
) d
But i need to convert CAST date CAST(0xA2380B00 AS Date) to varchar date
Is any possible?
Well, i don't really understand why you need to convert it to varchar, because if i'm not mistaken datetime behaving like string in most if not all of the situations.
However, you can use the DATE_FORMAT function. It outputs string, and you can format the datetime if you want.

MySQL NOW() Returns Only Year

MySQL docs about NOW() function...
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
Here's my query...
$sql = "
INSERT INTO `users` ( `username`, `password`, `email`, `time` )
VALUES ('{$username}', '" . sha1( $password ) . "', '{$email}', NOW() )
";
The problem is that in the database I don't have the full datetime, but only the year. Any solutions?
It's int(10) like it was when I used UNIX timestamp.
NOW() works for DATETIME fields only.
You need to either convert your field to DATETIME (the preferable method), or convert NOW() into a UNIX timestamp using UNIX_TIMESTAMP():
UNIX_TIMESTAMP(NOW())
I had this same trouble and was stumped for the longest time till I read this question. I used mysql's now() function to update a member's last login time.
MySQL's now() returns the date time as a string, like YYYY-MM-DD HH:MM:SS - somewhere along the line I stopped using this and started using time() (UNIXTIME) (makes it easier to work with) which returns a string of numbers, like 1352254528. But you can't store a string of numbers like this in a datetime field, and likewise, you can't store YYYY-MM-DD HH:MM:SS in an int(11) field. The latter will result in only the year being stored.
In the end you should use:
int(11) if you're going to use PHP's time() function to store the time as a string of numbers
or
DATETIME field if you're going to use MySQL's NOW() function.
try
strtotime(now())
And you should definitely use timestamp or datetime as a type for your column.

MySQL Date, Minutes Add to a value

I cant seem to get this to work, It returns Null
SELECT sdt, timeFor, DATE_ADD(TIMESTAMP(sdt), INTERVAL timeFor MINUTE) FROM tbl_day
The return keeps returning
sdt, timeFor, DATE_ADD(TIMESTAMP(sdt), INTERVAL timeFor MINUTE)
'0000-00-00 01:00:00', 15, ''
Columns Type
sdt DATETIME
timeFor BIGINT(20)
Any ideas
MySQL usually returns NULL on date/time operations when column value is incomplete datetime. Something like 2010-00-05 11:22:33, etc. Also using timestamp function on sdt column might not be a good idea. I'd suggest providing normal datetime value.