MySQL: Comparing the result of two convert functions - mysql

Im currently working on a database that have been initialize with dates in varchar instead of using datetime format.
I'm supposed to compare the dates of the DB with an input date (31/05/2020 actually).
Here is the part of my code that makes troubles :
AND Convert(datetime,t1.fin_contrat, 103) > Convert(datetime, '31/05/2020', 103 )
But I've an error from PhpMyAdmin which is : #1064 - Syntax error near 't1.fin_contrat, 103) >= Convert(datetime, '31/05/2020', 103 ) GROUP BY siren OR' line 1
According to this link : https://learn.microsoft.com/fr-fr/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15#implicit-conversions varchar should be able to being converted directly to datetime value.
I tried to use WHERE instead of AND it doesn't work. So I'm out of option, and i'm seeking for ideas.
Thanks for your help

In MySQL, you want something like this:
where date(t1.fin_contrat) = str_to_date('31/05/2020', '%d/%m/%Y')
This corresponds to the code in your question, but adapted for MySQL.

The fonction CONVERT is not the same betweeen Microsoft SQL. and Mysql
https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_convert
To convert a string. to a date. you must use STR_TO_DATE
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
ps:
Every sql server software ( Oracle , Redshift , Posgtres , Mysql , MariaDB , MS SQL server , Sqllite , ...) has his own SQL dialect . Usually simple queries are portable but more complex or using functions queries must be rewriten .

Related

Converting MSSQL stored procedures to MySQL - CONCAT issue

I have a question regarding the conversion of MSSQL stored procedures to MySQL ones.
I used http://www.sqlines.com/online to convert my MSSQL file to a MySQL syntactically valid format. There was an issue converting the following to MySQL:
set #InspectionNo = right('0' + convert(varchar(10),#i),2)
The converted output showed:
set v_InspectionNo = right(Concat('0' , convert(varchar(10),#i)),2)
which doesn't appear to be the correct syntax.
Any advice on this would be greatly appreciated, it's got me stumped!
Change it to the following. In mysql, you cannot cast to varchar, only char is supported.
set v_InspectionNo = right(Concat('0' , cast(#i AS char(10))),2)

SQL Query nvarchar to date

I am working on SAP HANA Studio and have tried to run SQL command that converts an entire column of field, nvarchar, into one of field, date.
My dates have format: dd-mon-yyyy (i.e '29-Mar-1997') with field nvarchar(11).
I have looked at previous questions and SQL command documentation (for functions like CAST, CONVERT, TO_DATE, STR_TO_DATE) and have not gotten a solution.
Typical errors I get are: Function not recognized, or, Error while parsing Service Date as DATE at function to_date().
Any suggestions?
Thanks
-Diana
Try TO_DATE():
select to_date(col, 'DD-MON-YYYY')
Obviously your database driver/layer in SAP HANA does not support all mySQL functions.
Please connect to your database directly (using command-line or a gui like HeidiSQL) and create a view in your database:
CREATE VIEW view_tablename AS
SELECT STR_TO_DATE(`Service Date`, '%d-%b-%Y') AS ServiceDateDt, * FROM tablename
Then use view_tablename instead of tablename in all your queries - because view_tablename has the additional date field "ServiceDateDt".

Converting Unix timestamp to actual Date and time

Is there any sql command which I can insert into the stated query so I can convert the timestamp. Although it could be done separately which I have seen so far but I am trying to find something which I can add to the stated query as that would be helpful because I am using other queries to retrieve the data as well. If you any other questions please do mention. Addition: rating_timestamp contains both time and date.
SELECT rating_id,
rating_postid,
rating_posttitle,
rating_rating,
rating_timestamp,
rating_username,
rating_userid
FROM wp_ratings;
In cases of date arithmetic, it is especially important to specify the DBMS you are using - Oracle's math is different from Postgres' math is different from SQL Server's math is different from MySQL's math is...
This assumes that you are using SQL Server. Since there is no built in command to do this conversion, you need to create your own function to do that. The function below takes a UNIX / Linux timestamp and converts it to an SQL Server datetime.
CREATE FUNCTION dbo.fn_ConvertToLocalDateTime (#unixdate BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE #UTCTimeOffset BIGINT
,#LocalDatetime DATETIME;
SET #UTCTimeOffset = DATEDIFF(second, GETUTCDATE(), GETDATE())
SET #LocalDatetime = DATEADD(second, #unixdate + #UTCTimeOffset, CAST('1970-01-01 00:00:00' AS datetime))
RETURN #LocalDatetime
END;
GO
I wast sure about about Sql version before. This worked perfectly for me.
FROM_UNIXTIME(rating_timestamp,'%h:%i:%s %D %M %Y')

converting java time to sqldate in query

java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.

SQL Server 2008 Datetime convert to seconds/miliseconds

--Datetime format = '2013-10-21 12:12:12.000'
--Obviously poor syntax but you can see what Im trying to do
select personnum, convert(decimal, shiftEndDate, 4)-convert(decimal, ShiftStartDate, 4) as ShiftInSeconds
from dbo.Time
where shiftStartDate between '2013-10-14 00:00:00.000' and '2013-10-20 23:59:59.000'
you can use DATEFROMPARTS to create date type from shiftEndDate and then cast you condition strings to date/datetime (sql server should do this automatically, if no - use CAST/CONVERT)