Server.: String Functions Provided by SQL
Server.: String Functions Provided by SQL
Date Functions:
The syntax is:
SELECT date_function(parameters)
FUNCTION DESCRIPTION
DATEADD(datepart, number, date) Adds the no.of dateparts to date.
DATEDIFF(datepart, date1, date2) Returns the number of dateparts
between two dates.
DATENAME(datepart, date) Returns the integer value of the date
part.
DATEPART(datepart,date) Returns the integer value of the date
part.
GETDATE() Returns the current date and time.
Examples:
SELECT GETDATE()
The above statement displays the current system date and time with the help
of the GETDATE function.
SELECT DATEDIFF(yy,ord_date,getdate())
This statement uses the DATEDIFF function to find the difference between the
current date and the order_date, from sales tables. The difference should be
expressed in terms of number of years.
The above statement uses the DATEPART function to return the year when the book
was published, along with the title name.
Data Conversion:
The CONVERT function is used to change data from one type to another when SQL
server cannot implicitly perform a conversion. Using the CONVERT function, data can
be modified in variety of styles.
CONVERT(datatype[(length), expression[,style])
SELECT Ytd_Sales=CONVERT(char(10),ytd_sales)
FROM titles.
Selecting Rows
There are situations in which only a few rows need to be retrieved from the
table based on a condition. The WHERE clause, is provided by SQL server, to specify
a condition.
Example:
Comparison operators like =, >, <, >=, <=, !=, !< and !>
Range operators like BETWEEN and NOT BETWEEN.
List operators line IN and NOT IN.
String operators like LIKE and NOT LIKE.
Unknown values like IS NULL and NOT NULL.
Logical operators like AND , OR and NOT.
Comparison Operator
The command syntax is:
Examples:
Range Operator
The range operator is used to retrieve data that can be extracted in ranges.
The range operations are:
BETWEEN
NOT BETWEEN
Examples:
1. SELECT title from titles WHERE advance BETWEEN 2000 AND 5000
2. SELECT title FROM titles
WHERE advance NOT BETWEEN 4000 AND 5000
List Operator
Examples:
Example Description
SELECT title FROM titles WHERE Returns all titles from titles table where first
type LIKE ‘bus%’ three characters of the column type are
‘bus’
SELECT * FROM publishers Returns all rows from publishers table where
WHERE country LIKE ‘US_’ country name is three characters long and
starts with US where the third character can
be anything.
SELECT title_id, price FROM titles Returns all columns from the titles table
WHERE title_id LIKE ‘P[SC]]%’ where title_id starts with the character P and
contains S or C in the second position
followed by any number of characters.
SELECT title_id, price FROM titles Returns all title_id and price from the titles
WHERE title_id, LIKE ‘P[^C]%’ table where title_id starts with P and does
not contain and S as the second character
and the third position onwards can contain
any characters.
Unknown Values
Unknown values refer to the data entered in the form of the NULL keyword.
In SQL serve terms, NULL is an unknown value or the value for which data is not
available. The rows containing the NULL values can be retrieved by using the IS
NULL keyword in the WHERE clause.
Example Description
SELECT * FROM publishers WHERE Returns all the rows specific to the
city=’Boston’ OR city=’Paris’ conditions, even if any one of the
conditions is true.
SELECT publishers WHERE Returns all the rows specific to the
city=’Boston’ AND city=’MA’ conditions, when both the conditions
are true.
SELECT * FROM publishers WHERE Returns all the rows specific to the
city=’Boston’ OR NOT city=’Paris’ conditions, except the rows specified
with the condition after NOT operator.
Distinct
The distinct clause removes duplicate rows from the result set. Duplicate rows
can be eliminated by using the DISTINCT keyword in the SELECT statement.
Syntax:
Example:
SELECT DISTINCT title_id
FROM titleauthor
WHERE title_id LIKE’PS%’
TOP n [PERCENT]
SELECT TOP 20
It the SELECT statement, including TOP, has and ORDER BY clause, then the rows
to be returned are selected after the ORDER BY statement has been applied.