Converting MSSQL stored procedures to MySQL - CONCAT issue - mysql

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)

Related

Converting raw laravel mysql json query into postgres syntax

I have the following query that works in MYSQL
$query->whereRaw('
ST_Distance_Sphere(
point(address->>"$.longitude", address->>"$.latitude"),
point(?, ?)
) * .000621371192 < ?
', [$longitude, $latitude, $distance]);
When i deployed to heroku im getting this error
SQLSTATE[42703]: Undefined column: 7 ERROR: column "$.longitude" does not exist
LINE 3: point(address->>"$.longitude", address->>"$.latitu...
Im using postgres on heroku and i know the issue is something to do with the json operator.
What is the $.<json key> syntax called? and how do i convert this raw sql query to postgres?
In the worse case i leaning on switching my database to mysql if i cant solve this by Wednesday as it wont be a big deal
Postgres uses double quotes as delimiter for columns, so you must use single quotes to indicate strings. Laravel will take care of it
st_distance_sphere(
st_point(address->>'$.longitude',address->>'$.latitude'),st_point(?, ?)
)

MySQL: Comparing the result of two convert functions

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 .

Delphi datatype for SQL Server last_user_update

The SQL Server code below returns the time-date of the last update to a table (any row.)
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE OBJECT_ID=OBJECT_ID('TableName')
AND (Index_ID = 1)
If I use this in a TFDQuery, what is the datatype FDQuery1.FieldByName('last_user_update')?
If I want to store and compare this value at different times in a Delphi variable, what Delphi datatype should I assign it to, Double?
You use a TDateTime for this.
Although the TDateTime is represented internally by a double you don't use a double because you'll miss out on all the date/time support.
The code goes like this.
var
LastUpdate: TDateTime;
begin
//Do query etc.
...
LastUpdate:= MyQuery.FieldByName('last_user_update').AsDateTime;
Note that SQL server 7 and before do not have support for TDate. So if you just want the date part this code will fail in SQL server 7.
LastUpdate:= MyQuery.FieldByName('last_user_update').AsDate;
Just get the full DateTime and strip of the Time part later.
However, you're working with 2008 so just extracting the date will work fine for you.
Here's a list of DateTime functions: http://www.delphibasics.co.uk/ByFunction.asp?Main=DatesAndTimes

Comparing string with SmallDateTime used to work

I'm trying to debug an old VB6 program at my job. It's full of bugs and hardcoded stuff.
The program is connecting on a SQL Server 2005 database to read and write info using stored procedures.
Since the program is still being used while I'm working on it, I made a backup of the database to test some things and restored it in my version of SQL Server 2008. Everything was working fine.
When I came in the next morning, I got an error I didn't have the previous night even though the program wasn't used in the night.
The problem :
"exec dbo.sp_Get_Wait_Data '" & DateEx & "'"
DateEx is a string containing "2012/06/14"
The stored procedure :
[sp_Get_Wait_Data] #Datest as char(10)
AS
SELECT
(A lot of column names here)
FROM
Fiche
LEFT JOIN
voyage ON fcid = vofiche
LEFT JOIN
client on fcaccount = cusnov
WHERE
fcdate = #Datest
AND (void is null or (void > 0 and (void <> 999 and void <> 1000 and void <> 998)))
AND ((fcremarques NOT LIKE '%SYSANNULATION%' OR
fcremarques IS NULL)
AND fcrettime IS NOT NULL)
ORDER BY
FcTime, FcOrgSite, fcdessite
The error message :
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value
So the error is here fcdate=#Datest in the stored procedure. I tried adding this line in the stored procedure
SELECT convert(datetime, #Datest, 120)
which worked like a charm for the convertion within the query but caused a few hundred other errors within the program when other queries tried to access this variable so it is not an option.
Any idea why this query was working perfectly yesterday and now it's giving me this error? Thanks in advance.
You probably have set dateformat dmy when connecting to the copy of your database. The date format is set implicitly by the language used. set language.
You could change the format of the parameter to YYYYMMDD which will be safe regardless of set dateformat or even better, change your parameter to datetime.
If that is not an option you can rewrite your query using where fcdate=convert(datetime, #Datest, 111)

opening MySQL query when the query selects columns of varchar datatype in delphi

I recently started with MySql, so i might be doing a begginers mistake. Any help is appreciated.
I connect the database in Delphi, define a query with columns of datatypes integer, decimal and varchar.
The problem is when I select a query in Delphi, and debug after opening query, the columns that are varchar does not appear as i if never selected them.
The driver for odbc connector is the latest mysql-connector-odbc-5.1.10
If you defined persistent fields for your TQuery in design mode, maybe you forgot to add the varchar (TStringField) field, or misspelled FieldName. Also make sure the filed is varchar rather than nvarchar (TWideStringField).
Another solution is to remove all persistent fields from your TQuery.
Speculating: Seems, you are using dbTables -> BDE -> ODBC -> MySQL data access path. BDE skips the fields with unknown data types. Probably Unicode character type is not supported by BDE.
Possible solutions:
try to set ANSI character set for the connection in ODBC connection parameters;
use dbGo / ADO data access components;
use dbExpress components with MySQL dbExpress driver;
use 3d party data access components.
Sample query:
SELECT anumber, adecimalnumber, avarchar FROM atable
Sample Delphi code (doing this from memory, so never mind small mistakes).
procedure Test;
var
AQuery: TADOQuery;
ANumber: integer;
ADecimal: Real;
AString: string;
begin
AQuery:= TAdoQuery.Create;
try
AQuery.Connection:= SomeODBCConnection;
AQuery.SQL.Text:= 'SELECT anumber, adecimalnumber, avarchar FROM atable';
AQuery.Open;
ANumber:= AQuery.FieldByName('anumber').AsInteger;
ADecimal:= AQuery.FieldByName('anumber').AsFloat;
AString:= AQuery.FieldByName('anumber').AsString; << Gets the varchar.
//Use AQuery.Next + test for AQuery.EOF to walk through rows.
finally
AQuery.Free;
end;
end;
It really not difficult at all to get varchar data.