Common ways to find sub string in oracle and sql server - sql

Hi I have below sql query where I am using SUBSTR in oracle, I need to modify SUBSTR so that this query should work for both oracle and sql server
Note: in oracle we have SUBSTR and in sql server we have SUBSTRING, LEFT, RIGHT but I need common query that should work in both oracle an sql server data base.
SELECT INA.LOCATION_CODE,INA.ACCT_NUM,INA.STATE_ENTRY_DATE,FP.NUM_INA_DYS_BF_PUR FROM INACTIVE_ACCOUNT INA
LEFT JOIN FUNCTIONAL_AREA FA ON INA.LOCATION_CODE=FA.LOCATION_CODE AND SUBSTR(INA.RETURN_TO_STE_CODE,0,1)=FA.FUNC_STATE_ENTR_KY
JOIN FUNC_AREA_PARAM FP ON FA.GUID=FP.PARN_GUID;

There isn't such a function. Trying to write code that works across different databases with exactly the same syntax is a fool's errand. There are too many subtle differences between most databases that it is generally not possible.
That said, you can do something in this case:
ON INA.LOCATION_CODE = FA.LOCATION_CODE AND
INA.RETURN_TO_STE_CODE LIKE CONCAT(FA.FUNC_STATE_ENTR_KY, '%')
This should work in both databases. That is because you are lucky.

Related

SQL query with two different SQL istances

I would like to know how to make a select query in vb.net, with joins between tables contained in databases of two different instances of sql server.
thanks.

SQL: Trim string at spaces and difference between DBMS, SQL and MySQL

I was practicing on w3schools and used the Customers table (you can select it on the right hand side). As for which DBMS I'm using, I'm not sure. The site says "Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems." - is the syntax between those DBMS the same or very similar?
I want to extract only the first name of the customers. I thought this should do the trick:
SELECT LEFT(ContactName, CHARINDEX(' ',ContactName)-1)
FROM Customers;
It doesn't work, however, it returns nothing. As in, it doesn't process it, in the output window it shows me the start page, so there's no error message. I don't really get the actual reason behind it, because when I put CHARINDEX by itself in the SELECT statement it gives me the expected result.
Next question would've been how I deal with multiple spaces (when someone has a middle name), but since I got stuck beforehand I didn't get to this part.
SQL
Sql is a language used to query relational database systems.SQL full form is structured query language.
Sql is like update,delete,find, etc.
sql basically using multithreading concept where oracle using multiprocessor oracle using for high database handling like banking
etc.
Sql language is used in oracle as a writing language. 5 .In SQL server there is no transaction control.
MySQL:
MySQL is a also a Database tool itself that uses SQL language. It is open source.
MySQL is weaker in the areas of inserting and deleting data. But it is an excellent choice, for data storage and referencing data.
MySQL is a relational database management system. You can submit SQL queries to the MySQL database to store, retrieve, modify or delete
data.
Basically, MySQL is one of many books holding everything, SQL is how you go about reading that book.
DataBase Management System(DBMS):
A database management system (DBMS) is a computer software application that interacts with the user, other applications, and the
database itself to capture and analyze data.
A database management system (DBMS) is a collection of programs that manages the database structure and controls access to the data
stored in the database.
DBMSs include MySQL, PostgreSQL,Microsoft SQL Server, Oracle, Sybase and IBM DB2.
Sometimes a DBMS is loosely referred to as a database.
The Syntax Between them are very similar.
Actually your selection of column was wrong you were selecting ContactName by customer name index secondly you don't need to minus its index. This will return first name of customer.
SELECT LEFT(ContactName, CHARINDEX(' ',ContactName))
FROM Customers;
I notice that you are using two different columns in the query: ContactName and CustomerName. Why is that?
I suppose your query should work if written like this:
SELECT
LEFT(ContactName, CHARINDEX(' ', ContactName) - 1) AS FirstName
FROM
Customers;
It should also work using SUBSTRING, as follows:
SELECT
SUBSTRING(ContactName, 1, CHARINDEX(' ', ContactName) - 1) AS FirstName
FROM
Customers;
Hope this helps.

Simple SQL query that works for Oracle and SQL Server

I am looking for a small and simple query that works on Oracle and SQL Server.
It is used as a test query to check the connection.
For SQL Server we used SELECT 1, but in Oracle it would have to be SELECT 1 FROM DUAL.
What we plan to use now is SELECT COUNT(*) FROM (sometable) but any ideas for an even simpler query are appreciated.
One simple option is to add a view to SQL Server called DUAL that just returns 1, that way you can have a simple query that works the same in both environments:
SELECT 1 FROM DUAL
If returning data from relations isn't important then:
SELECT 'Hello world';
will rely on a connection as much as anything else. Your RDBMS should return 'Hello world'. Tested on SQL server and PostgreSQL (don't have access to Oracle).
As long as you write query in ANSI standard. It can be executed in all the RDMS.
May be you can try this query....
select ColumnName from TableName where 1=2
BTW, the DBProvider shld have come property to state of DB connectivity... which DB provider does ur application uses?
Does it need to be a query? You could create a simple stored procedure with the same name in both databases, that just returns a constant, and execute it from the application server.

will the sql queries i run in ms-access also work on mysql without any changes?

will the sql queries i run in ms-access also work on mysql without any changes ?
It's possible, but it depends on what the queries use. Date and string functions are the most likely to cause problems when porting queries.
The DATEDIFF keyword is supported on both Access & MySQL, but the function takes different parameters:
Access: DATEDIFF
MySQL: DATEDIFF
Well, if the coder wrote the queries with portability in the forefront of their mind then there's a good chance that you will need to make only minimal changes. However, you could only expect the most simple queries to work with no changes, regardless of which SQL product were involved.
In an ideal world, all SQL products would comply with ISO/ANSI Standard SQL with vendor extensions. In reality, while mySQL generally has a good track record in Standard SQL compliance, the Access Database Engine's record is rather poor -- it still doesn't even conform to entry level SQL-92, which was a fairly fundamental requirement even a decade ago (and seemingly none too difficult to achieve either).
[Your question is in all lower case. I've assumed by 'queries' you mean SQL DML SELECT. If you use 'queries' to mean INSERT/UPDATE/DELETE SQL DML plus SQL DDL and SQL DCL then this changes the answer. You should note the the Access Database Engine's UPDATE SQL DML is proprietary and non-deterministic; further, it does not support SQL-92's scalar subquery syntax. This is of major significance when porting to a SQL product.]
Thanks for your question. It just goes to show that it's worth considering portability from day one.
I would like to add one more point to OMG Ponies answer
Transform that is use for cross tab queries in MS ACCESS cannot be used in MySQL
e.g.
TRANSFORM Sum([M_Sales].[Amount]) AS SumOfAmount
SELECT [M_Sales].[Department]
FROM M_Sales
GROUP BY [M_Sales].[Department]
PIVOT Format([M_Sales].[Sale_date],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
in MSACCESS ( taken from )
could be something in MySql Common MySQL Queries . Just visit the Pivot table section
Given some of your previous questions, you could save some time with MySQL, compared to Access: 12.1.10. CREATE TABLE Syntax

sql select thru multiple postgres databases

is there a way to create a select statement that would retrieve data from multiple database in postgre?
i was thinking it would be something like this:
select * from dbname1.table1, dbname2.table2 where dbname1.table1.column1 = dbname2.table2.column1
Have a look at the "dblink" contrib module.
OTOH it's possible that you treat the databases in a PostgreSQL cluster as equivalent to the databases in... let's say MySQL. Which is incorrect - the PostgreSQL databases contain schemas and those are the equivalent of the databases in MySQL.
From here:
It is not possible to access more than
one database per connection.
Update: but see Milen's answer.