User Tools

Site Tools


database:mysql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
database:mysql [2023/11/02 10:56] – [Mysql basic commands] skipidardatabase:mysql [2023/11/02 11:04] (current) skipidar
Line 517: Line 517:
 == IFNULL(), ISNULL(), COALESCE(), and NVL() Functions == == IFNULL(), ISNULL(), COALESCE(), and NVL() Functions ==
  
-| ''IFNULL(COLUMN, DEFAULTVALUE) -> COLUMN or DEFAULT'' | The IFNULL() function returns a specified value if the expression is NULL. |+| ''IFNULL(COLUMN, DEFAULTVALUE) -> COLUMN_VALUE or DEFAULT'' | The IFNULL() function returns a specified value if the expression is NULL. | 
 +| ''ISNULL(COLUMN, DEFAULTVALUE) -> BOOL'' | Return boolean. |
 | ''COALESCE(val1, val2, ...., val_n)'' | Return the first non-null value in a list:  | | ''COALESCE(val1, val2, ...., val_n)'' | Return the first non-null value in a list:  |
  
Line 525: Line 526:
 SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0)) SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))
 FROM Products; FROM Products;
 +
 +
 +--- using boolean ISNULL
 +SELECT OrderID, 
 +CASE
 +    WHEN ISNULL(AmountDue) THEN "Paid in full"
 +    WHEN DATE(DueDate) < date(NOW()) THEN "Order is past due"
 +    ELSE CONCAT("Order is due on ", CONVERT(VARCHAR, DueDate, 121))
 +END
 +FROM OrderDetails;
 +
 </sxh> </sxh>
 +
 +
 +
 +
 +
 +== Stored Procedure Example ==
 +
 +
 +<sxh sql>
 +
 +--- procedure
 +CREATE PROCEDURE SelectAllCustomers
 +AS
 +SELECT * FROM Customers
 +GO;
 +
 +--- execute
 +EXEC SelectAllCustomers;
 +
 +
 +
 +--- procedure with args
 +CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
 +AS
 +SELECT * FROM Customers WHERE City = @City
 +GO;
 +
 +--- exec
 +EXEC SelectAllCustomers @City = 'London';
 +
 +</sxh>
 +
  
  
database/mysql.1698922582.txt.gz · Last modified: by skipidar