Saturday, August 10, 2024
SQL Wildcard Characters
A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator with WHERE
clause to search for a specified pattern in a column.
Example:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
The above example shows - get all the customers whose name starts with 'a'
Wildcard characters:Symbol | Description |
---|---|
% | Represents zero or more characters |
_ | Represents a single character |
[] | Represents any single character within the brackets * |
^ | Represents any character not in the brackets * |
- | Represents any single character within the specified range * |
{} | Represents any escaped character ** |
Using the % Wildcard
The %
wildcard represents any number of characters, even zero characters.
Return all customers that ends with the pattern 'es':
SELECT * FROM Customers
WHERE CustomerName LIKE '%es';
Using the _ Wildcard
The _
wildcard represents a single character.
It can be any character or number, but each _
represents one, and only one, character.
City
starting with any character, followed by "ondon":WHERE City LIKE '_ondon';
Using the [] Wildcard
The []
wildcard returns a result if any of the characters inside gets a match.
Return all customers starting with either "b", "s", or "p":
SELECT * FROM Customers
WHERE CustomerName LIKE '[bsp]%';
Using the - Wildcard
The -
wildcard allows you to specify a range of characters inside the []
wildcard.
Return all customers starting with "a", "b", "c", "d", "e" or "f":
SELECT * FROM Customers
WHERE CustomerName LIKE '[a-f]%';
Friday, August 9, 2024
SQL NULL functions
NVL() function
NVL() function is used to provide a default value when the specified field is null. we can also use the COALESCE()
function as alternative.
Syntax:
NVL(column_name, default_value)
For example
In the below table, unitOnOrder is optional and can be null.
P_Id | ProductName | UnitPrice | UnitsInStock | UnitsOnOrder |
---|---|---|---|---|
1 | Jarlsberg | 10.45 | 16 | 15 |
2 | Mascarpone | 32.56 | 23 | |
3 | Gorgonzola | 15.67 | 9 | 20 |
SELECT ProductName, UnitPrice * (UnitsInStock + UnitsOnOrder)
FROM Products;
In the example above, if any of the "UnitsOnOrder" values are NULL, the result will be NULL. To avoide such nullibility issue, we use NVL() as below:
SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOnOrder, 0))
FROM Products;
SQL Case Expression
CASE
expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE
clause.If there is no
ELSE
part and no conditions are true, it returns NULL.WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
SQL Sequence
CREATE SEQUENCE
syntax:MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence)
VALUES (seq_person.nextval,'Lars','Monsen');
SQL Operators
Using operators
- + (Addition)
- - (subtraction)
- * (Multiplication)
- / (Division)
- % (Modulus) - it returns reminder.
- = (equal to)
- != (Not equal to) or <>
- > (Greater than)
- < (Less than)
- >= (Greater than or equal to)
- <= (Less than or equal to)
- AND (To check for both conditions to be true)
- OR (To check for one of the conditions to be true)
- NOT (To negate the given condition)
WHERE NOT Country = 'Spain';
WHERE CustomerName NOT LIKE 'A%';
- IN (To specify multiple possible values for a column
WHERE Country IN ('Germany', 'France', 'UK');
- BETWEEN (Between a certain range)
WHERE Price BETWEEN 10 AND 20;
- ALL (TRUE if all of the subquery values meet the condition)
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
- LIKE (Search for a pattern)
WHERE CustomerName LIKE 'a%';
- ANY (TRUE if any of the subquery values meet the condition)
- EXIST
- SOME
- & (Bitwise AND)
- | (Bitwise OR)
- ^ ( Bitwise exclusive or)
Featured Post
11g to 12c OSB projects migration points
1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper: File⇾Import⇾Service Bus Resources⇾ Se...
-
Please find the following links for OIC hands on and relevant information: Oracle Integration Cloud Introduction | Benefits | Services offer...
-
What is Throttling: Throttling is termed as "regulate the flow". Oracle Service Bus has throttling feature and by using that f...
-
OIC interview Q & A: 1. FBDI approach with an example https://soalicious.blogspot.com/2022/02/oic-erp-supplier-bulk-import-and.html 2. O...