Sql Cast hexadecimal date to mysql varchar(date) - mysql

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.

Related

Datetime field overflow error in 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.

Most efficient way of flooring todays date

This seems like overkill but is the only way I have been able to floor todays datetime to 00:00:00.000 at database level:
select CAST(FLOOR(CAST(CURRENT_TIMESTAMP AS float)) AS DATETIME)
I have tried using:
select FLOOR(getdate())
But get the following message:
Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
Can anyone recommend another way of doing this?
Since you are using SQL Server 2008 you could make use of the date data type.
declare #Today date
set #Today = getdate()
select #Today
Or without the variable.
select cast(getdate() as date)
If you need to have the value as a datetime just cast it back to a datetime.
select cast(cast(getdate() as date) as datetime)
There are a lot of ways of doing this i have seen the floor one before. Here are a few more.
select cast(cast(CURRENT_TIMESTAMP as date) as datetime)
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SELECT CAST(CAST(CURRENT_TIMESTAMP - 0.50000004 AS int) AS datetime)
I normaly do the Cast to date version.

SQL Server convert function query

I have used a convert function in one of my Queries
Select * from tblMas where FromDate >= CONVERT(varchar(10), #FromDate,112)
#FromDate is a Datetime parameter, I don't know why but this query works fine.
Though it should not as FromDate is of DateTime field and we are comparing it with Varchar field.
CONVERT(varchar(10), #FromDate, 112) returns result in yyyyMMdd format. I am lost how SQL Server is comparing this and returning right result.
There will be an implicit type conversion from varchar to datetime before the comparison.
If you have a look at the queryplan you will see something like this.
<ScalarOperator ScalarString="[FromDate]>=CONVERT_IMPLICIT(datetime,CONVERT(varchar(10),[#FromDate],112),0)">
Another way to remove the time part from a datetime.
select *
from tblMas
where FromDate >= dateadd(day, datediff(day, 0, #FromDate), 0)
In SQL Server 2008 you can use
select *
from tblMas
where FromDate >= cast(#FromDate as date)

format date to 105 in SQL Server after casting

I have a problem: I have a datetime and I need the date to specific format
So I just casted datetime to time
SELECT CAST (GETDATE() AS DATE) -- result (2011-06-08)
and for formatting I use convert
SELECT CONVERT(DATE, CAST (GETDATE() AS DATE), 105) --result (2011-06-08)
105 format (dd-mm-yy)
but, the result of both is same,
CONVERT is not working for 105 formatting,
Any ideas?
thanks
To get the results you're looking for you need to convert the DATE to a VARCHAR like this:
SELECT CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105)
If you cast to a DATE, you will always get the full DATE.
You can truncate the date by re-casting to the DATE type.
SELECT CAST(CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105) as DATE)
You can use as
SELECT Convert(varchar, getdate(), 105)
select convert(varchar(50),date,105) as Date

Creating DATETIME from DATE and TIME

Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?
Copied from the MySQL Documentation:
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
To get a true DATETIME value from your two separate DATE and TIME values:
STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
You could use ADDTIME():
ADDTIME(CONVERT(date, DATETIME), time)
date may be a date string or a DATE object.
time may be a time string or a TIME object.
Tested in MySQL 5.5.
datetime = CONCAT(date, ' ', time);
select timestamp('2003-12-31 12:00:00','12:00:00');
works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.
select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
Without creating and parsing strings, just add an interval to the date:
set #dt_text = '1964-05-13 15:34:05.757' ;
set #d = date(#dt_text) ;
set #t = time(#dt_text) ;
select #d, #t, #d + interval time_to_sec( #t ) second;
However this truncates the microseconds.
I agree with Muki - be sure to take account of time zones and daylight savings time!