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 .
I am trying to convert Oracle table data into JSON files. I have three databases and the below code gives output as JSON file in one DB but the other two databases throw ORA-00907: missing right parenthesis error.
Syntactically it is correct, as it gave output in one DB. Don't understand what is going wrong.
This is in Oracle DB, How do I find out which version of Oracle is installed in those DB's and if they are 12.2 and above, Is there a way to fix this issue? All I want is to convert the output of a select statement to a json file. Thanks in advance
code:
SELECT JSON_OBJECT ( 'empid' value eid , 'name' value ename , 'add' value eaddr )
FROM abc.emp
JSON_Object is available from Oracle version 12.2 .
Run the query Select * from v$version to check your oracle version
Im trying to Establish a One Direction Sync from a specific query'd recordset from MSSQL (express 2008) to Mysql. Here is that query.
SELECT [datafk]
,[datahistorypk]
,[date]
,[displayText]
FROM [FCentral].[dbo].[DataHistory]
WHERE [sampleNr] =
(SELECT MAX (sampleNr) FROM [FCentral].[dbo].[DataHistory])
This results in a Multiple results. I need to insert each of those results into my "Linked Server" remotely connected MySQL DB Table.
This code works from SSMS and does insert into my MySQL DB.
EXEC (' INSERT INTO `farms`.`CCData` (
`datafk` ,`datahistorypk` ,`date` ,`displayText` )
VALUES ("222", "13", "2017-10-19 14:25:05", "TEST"); ')
at BPF_REMOTE
Ultimately i will need to schedule this query to run automatically, It would be nice if it could run if a change was detected in the MSSQL table but that may be outside of my abilities.
I feel like im close, im just struggling to get the syntax right to convert from MSSQL to MySQL. Can anyone either point to a good example or help me with this query?
Set up a linked server in SQL Server and just do a regular insert:
INSERT INTO BPF_REMOTE.farms.CCData(datafk, datahistorypk, date, displayText)
SELECT [datafk], [datahistorypk], [date], [displayText]
FROM [FCentral].[dbo].[DataHistory]
WHERE [sampleNr] = (SELECT MAX (sampleNr) FROM [FCentral].[dbo].[DataHistory]);
I just Create a connection to my Database ( MYSQL ) Using ODBC witch use The mysql API and i start using Tdatabase Component and Ttable Component and Datasource and Dbgrid and i notice that Fields of Types like ( Varchar and LongVarchar and text . ect )
the Ttable Component can't import Those type of mysql they just import the basic numeric types like ( int and float ect ) but how can i get values like string .
thak you .. sorry for my English
Problem Solved
1 TSQLQUERY , and this is the one that i insert SQL filters.
1 DATASETPROVIDER connected to the TSQLQUERY.
1 CLIENTDATASET connected to the DATASETPROVIDER.
Now its the same as ACCESS DB's, 1 DBGRID connected to a TDataset, and that TDataset is connected to the previous CLIENTDATASET.
I have a MySQL server as a linked server in Microsoft SQL Server 2008. For the link I use MySQL ODBC Connector version 5.1.8. When invoking queries using OPENQUERY (the only way I found of performing queries), problems occur. Simple queries, such as
SELECT * FROM OPENQUERY(MYSQL, 'SHOW TABLES')
work fine. Selection of individual columns, e.g.,
SELECT * FROM OPENQUERY(MYSQL, 'SELECT nr FROM letter')
works fine as well, but SELECT * syntax does not work. The query:
SELECT * FROM OPENQUERY(MYSQL, 'SELECT * FROM mytable')
raises an error:
Msg 7347, Level 16, State 1, Line 6
OLE DB provider 'MSDASQL' for linked
server 'MYSQL' returned data that does
not match expected data length for
column '[MSDASQL].let_nr'. The
(maximum) expected data length is 40,
while the returned data length is 0.
How can I make the SELECT * syntax work?
This problem happens if you are querying a MySQL linked server and the table you query has a datatype char(). This means fixed length and NOT varchar(). This happens when your fixed length field has a shorter string than the maximum length that SQL Server expected to get from the ODBC.
To fix this go to your MySQL server and change the datatype to varchar() leaving the length as it is. Example change char(10) to varchar(10).
Executing the following command before queries seems to help:
DBCC TRACEON(8765)
The error messages go away and queries seem to be working fine.
I'm not sure what it does though; I found it here: http://bugs.mysql.com/bug.php?id=46857
Strangely, SQL Server becomes unstable, stops responding to queries and finally crashes with scary-looking dumps in the logs a few minutes after several queries to the MySQL server. I am not sure if this has to do anything with the DBCC command, so I'm still interested in other possible solutions to this problem.
What I did to fix this since I can't modify the MySQL database structure is just create a view with a cast ex: CAST(call_history.calltype AS CHAR(8)) AS Calltype,
and select my view from MSSQL in my linked server.
The reason behind is that some strange types don't work well with the linked server (in my case the MySQL enum)
I found this
"The problem is that one of the fields
being returned is a blank or NULL CHAR
field. To resolve this in the Mysql
ODBC settings select the option "Pad
CHAR to Full Length"
Look at the last post here
An alternative would be to use the trim() function in your SELECT statement within OPENQUERY. The downside is you have to list each field individually, but what I did was create a view that calls OPENQUERY and then perfrom select * on the view.
Not ideal, but better than changing data types on tables!
Here is a crappy solution I came up with because I am unable to change the datatype to varchar as the db admin for the MySQL server is afraid it will cause issues with his scripts.
in my MySQL select query I run a case statement checking the character length of the string and add a filler character in front of the string "filling it up" to the max (in my case its a char(6)). then in the select statement of the openquery I strip the character back off.
Select replace(gradeid,'0','') as gradeid from openquery(LINKEDTOMYSQL, '
SELECT case when char_length(gradeid) = 0 then concat("000000", gradeID)
when char_length(gradeID) = 1 then concat("00000", gradeID)
when char_length(gradeID) = 2 then concat("0000", gradeID)
when char_length(gradeID) = 3 then concat("000", gradeID)
when char_length(gradeID) = 4 then concat("00", gradeID)
when char_length(gradeID) = 5 then concat("0", gradeID)
else gradeid end as gradeid
FROM sometableofmine')
it works but it probably is slower...
maybe you can make a MySQL function that will do the same logic, or come up with a more elegant solution.
I had the similar problem to this myself, which I resolved by wrapping the column-names in single ` style quotes.
Instead of...
column_name
...use...
`column_name`
Doing this helps the MySql query-engine should the column-name clash with a key or reserved-word.*
Instead of using SELECT * FROM TABLE_NAME, try to use all column names with quotes:
SELECT `column1`, `column2`, ... FROM TABLE_NAME
Example for normal datatype columns
SELECT * FROM OPENQUERY(MYSQL, 'SELECT `column1`, `column2`,...,`columnN` FROM mytable')
Example for ENUM datatype columns
SELECT * FROM OPENQUERY(MYSQL, 'SELECT `column1`, trim(`column2`) `column2`, `column3`,...,`columnN` FROM mytable')
*For those used to Sql Server, it is the MySql equivalent of wrapping a value in square-brackets, [ and ].