Query comparing dates in SQL

Instead of ‘2013-04-12’ whose meaning depends on the local culture, use ‘20130412’ which is recognized as the culture invariant format. If you want to compare with December 4th, you should write ‘20131204’. If you want to compare with April 12th, you should write ‘20130412’. The article Write International Transact-SQL Statements from SQL Server’s documentation explains how to … Read more

ORA-12560: TNS:protocol adaptor error

o to the windows machine that hosts the Oracle database server Go to Start -> Run -> Services.msc in Windows. Locate OracleService < SID > (here OracleServiceORCL) and click on Start to start the oracle database service (if not already running) Once it is up and running, from the command prompt run the following:tnsping < tnsalias > (tnsalias entry you can … Read more

SQL Server FOR EACH Loop

SQL is primarily a set-orientated language – it’s generally a bad idea to use a loop in it. In this case, a similar result could be achieved using a recursive CTE:

How can I return pivot table output in MySQL?

This basically is a pivot table. A nice tutorial on how to achieve this can be found here: http://www.artfulsoftware.com/infotree/qrytip.php?id=78 I advise reading this post and adapt this solution to your needs. Update After the link above is currently not available any longer I feel obliged to provide some additional information for all of you searching for mysql pivot … Read more

CREATE VIEW must be the only statement in the batch

Just as the error says, the CREATE VIEW statement needs to be the only statement in the query batch. You have two option in this scenario, depending on the functionality you want to achieve: Place the CREATE VIEW query at the beginningCREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine; with ExpAndCheapMedicine(MostMoney, MinMoney) as ( select max(unitprice), min(unitprice) … Read more

How do composite indexes work?

Composite indexes work just like regular indexes, except they have multi-values keys. If you define an index on the fields (a,b,c) , the records are sorted first on a, then b, then c. Example:

When to use SELECT … FOR UPDATE?

The only portable way to achieve consistency between rooms and tags and making sure rooms are never returned after they had been deleted is locking them with SELECT FOR UPDATE. However in some systems locking is a side effect of concurrency control, and you achieve the same results without specifying FOR UPDATE explicitly. To solve this problem, Thread … Read more

How to Select Top 100 rows in Oracle?

Assuming that create_time contains the time the order was created, and you want the 100 clients with the latest orders, you can: add the create_time in your innermost query order the results of your outer query by the create_time desc add an outermost query that filters the first 100 rows using ROWNUM Query: UPDATE for Oracle 12c … Read more