SQL Date Functions



SQL Date Functions

Important built-in date functions in MySQL:

  • NOW() : Returns the current date and time
  • CURDATE() : Returns the current date
  • CURTIME() : Returns the current time
  • DATE() : Extracts the date part of a date or date/time expression
  • EXTRACT() : Returns a single part of a date/time
  • DATE_ADD() : Adds a specified time interval to a date
  • DATE_SUB() : Subtracts a specified time interval from a date
  • DATEDIFF() : Returns the number of days between two dates
  • DATE_FORMAT() : Displays date/time data in different formats

Built-in date functions in SQL Server:


  •    GETDATE() : Returns the current date and time
  •    DATEPART() : Returns a single part of a date/time
  •    DATEADD() : Adds or subtracts a specified time interval from a date
  •    DATEDIFF() : Returns the time between two dates
  •    CONVERT() : Displays date/time data in different formats

Data types for storing a date or a date/time value in the database:


  •    DATE: format YYYY-MM-DD
  •    DATETIME: format: YYYY-MM-DD HH:MI:SS
  •    TIMESTAMP: format: YYYY-MM-DD HH:MI:SS
  •    YEAR: format YYYY or YY

Data types for storing a date or a date/time value in the database:


  •     DATE: format YYYY-MM-DD
  •     DATETIME: format: YYYY-MM-DD HH:MI:SS
  •     SMALLDATETIME: format: YYYY-MM-DD HH:MI:SS
  •     TIMESTAMP: format: a unique number

SQL Transaction

SQL Transaction

Transactions are units of work accomplished in a logical order, whether in a manually by a user or automatically by some sort of a database program. A transaction is the propagation of one or more changes to the database.
Practically, you will club many SQL queries into a group and you will execute all of them together as a part of a transaction.

Properties of Transactions:

Transactions have four standard properties, usually referred to by the acronym ACID:

Atomicity: certifies that all operations within the work unit are completed successfully; otherwise, the transaction is terminated at the point of failure, and previous operations are rolled back to their former state.

Consistency: ensures that the database properly changes states upon a successfully committed transaction.

Isolation: enables transactions to operate independently of and transparent to each other.

Durability: ensures that the result or effect of a committed transaction persists in case of a system failure.
Transaction Control

There are following commands used to control transactions:

COMMIT: to save the changes.

ROLLBACK: to rollback the changes.

SAVEPOINT: creates points within groups of transactions in which to ROLLBACK

SET TRANSACTION: Places a name on a transaction.

Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only. They cannot be used while creating tables or dropping them because these operations are automatically committed in the database.

The COMMIT Command:

The COMMIT command is the transactional command used to save changes invoked by a transaction to the database. The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.

Syntax:
COMMIT;

The ROLLBACK Command:

The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. The ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

Syntax:
ROLLBACK;

The SAVEPOINT Command:

A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain point without rolling back the entire transaction.

Syntax:
SAVEPOINT SAVEPOINT_NAME;

This command serves only in the creation of a SAVEPOINT among transactional statements. The ROLLBACK command is used to undo a group of transactions.

Syntax:
ROLLBACK TO SAVEPOINT_NAME;

The RELEASE SAVEPOINT Command:

The RELEASE SAVEPOINT command is used to remove a SAVEPOINT that you have created.
Syntax:
RELEASE SAVEPOINT SAVEPOINT_NAME;

Once a SAVEPOINT has been released, you can no longer use the ROLLBACK command to undo transactions performed since the SAVEPOINT.

The SET TRANSACTION Command:

The SET TRANSACTION command can be used to initiate a database transaction. This command is used to specify characteristics for the transaction that follows.
For example, you can specify a transaction to be read only, or read write.

Syntax:
 SET TRANSACTION [ READ WRITE | READ ONLY ];

SQL Aggregate/ Scalar Functions


SQL Aggregate/ Scalar Functions

SQL built-in functions Used in calculations on data.

SQL Aggregate Functions

SQL aggregate functions return a single value, calculated from values in a column.
SQL aggregate functions:
  • AVG() - Returns the average value
  • COUNT() - Returns the number of rows
  • FIRST() - Returns the first value
  • LAST() - Returns the last value
  • MAX() - Returns the largest value
  • MIN() - Returns the smallest value
  • SUM() - Returns the sum

SQL Scalar functions

SQL scalar functions return a single value, based on the input value.
SQL scalar functions:
  • UCASE() - Converts a field to upper case
  • LCASE() - Converts a field to lower case
  • MID() - Extract characters from a text field
  • LEN() - Returns the length of a text field
  • ROUND() - Rounds a numeric field to the number of decimals specified
  • NOW() - Returns the current system date and time
  • FORMAT() - Formats how a field is to be displayed

Joins In SQL


The SQL Joins is used to combine records from two or more tables in a database. A JOIN is a means for joining fields from two tables by using values common to each. It is visible that the join is performed in the WHERE clause. Several operators can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used to join tables. However, the most common operator is the equal symbol.
SQL Join Types:
There are different types of joins available in SQL:
·        INNER JOIN: returns rows when there is a match in both tables.
·        LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
·        RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.
·        FULL JOIN: returns rows when there is a match in one of the tables.
·   SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.
·        CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables.


Useful Functions in SQL


Following is the list of all useful SQL built-in functions:


  •    COUNT - The SQL COUNT aggregate function is used to count the number of rows in a database table.
  •    SQRT - This is used to generate a square root of a given number.
  •    RAND - This is used to generate a random number using SQL command.
  •    MAX - The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column.
  •    MIN - The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
  •    CONCAT - This is used to concatenate any string inside any SQL command.
  •    Numeric - Complete list of SQL functions required to manipulate numbers in SQL.
  •    String - Complete list of SQL functions required to manipulate strings in SQL.
  •    AVG - The SQL AVG aggregate function selects the average value for certain table column.
  •    SUM - The SQL SUM aggregate function allows selecting the total for a numeric column.

Why we can't execute a stored procedure from a User Defined function(UDF)

Functions cannot "touch" any database but read them only. Stored procedures can do anything and everything with databases. You ...