Fix SQL query on MySQL and MsSQL