Handout 6 - Advanced Data Types, DateTime Functions, Views
Handout 6 - Advanced Data Types, DateTime Functions, Views
doc
Haramaya University
College of Computing and informatics
RECOMMENDED TEXTS:
1 Overview
This handout deals with some miscellaneous SQL topics. It covers some more advanced data
types to supplement the basic types already covered as well as functions that can be used to
work with date-time values.
It also covers views and stored procedures.
varchar(n) National character Variable-length Unicode data where the Depends on the number of
varying maximum length is n; n must be in the characters entered (1
range 1 to 4000. character = 2 bytes)
If n is not specified, the default is 1.
ntext Variable length Unicode data, 2 times the number of
maximum length is (2^30) – 1 characters entered
characters.
The drawbacks of storing data in ASCII format are that some characters may have different
bit patterns on different code pages and that some characters may appear on one code page
but not on another. In this case, when data is transferred between systems that have different
code pages, the data must be converted from the code page of the source system to that of the
receiving system. Even with the conversion, some character data may change or there may be
unknown characters.
This can be a problem when building a system, e.g. a database, that must support multiple
languages. This is also more of a problem for internet applications, where there may be many
different clients receiving data from a central system.
This problem has been addressed by the use of the Unicode Character System (UCS). This
system uses 2 bytes for each character – this allows for 65,356 different bit patterns, which is
sufficient to cover all major business languages.
If the character data in a system is stored using Unicode, and it is transferred to another
system that uses Unicode, then there will be no conversion problems such as loss of data.
In SQL Server, the char, varchar and text data types store characters in ASCII format.
SQL Server has 'collations' – a collation includes the code page to use, as well as sort orders
for character data. The collation can be specified for the database server but can also be set at
the database level and for individual columns in tables.
The nchar, nvarchar and ntext data types store characters in the Unicode format. This means
that twice as many bytes are required to store values of these types, as for the char, varchar
and text equivalents. However, if the system is being used to store data in many languages, or
if the data may be exported to other systems, then it is worth using the nchar, nvarchar and
ntext types.
Therefore, the national character types (nchar, nvarchar, ntext) should be used in systems
where many languages or alphabets are being used or where data may need to be transferred
to other systems.
Page 2 of 11
CCI – Computer Science Dept 655445337.doc
It is likely that Microsoft will incorporate the latest Unicode encoding into SQL Server,
through a Service Pack for SQL Server 2000 and in the next version of SQL Server.
4 DateTime Functions
The DateTime data type in SQL Server stores a data as a full date and time value. If you do
not specify the time, the date is stored with a time of 00:00:00.
There are a number of functions available for dealing with date-time values in SQL Server.
These specific functions are not SQL92/99 standard, but you will find similar functions in
other DBMS applications.
The SQL Server functions are listed in the table below.
Page 3 of 11
CCI – Computer Science Dept 655445337.doc
Year Returns an integer representing the Year If today is 31st December 2003:
(mm or m) part of the specified date. Select year (getdate())
This the same as datepart(yy, date) = 2003
When working with dates, e.g. inserting date values or comparing date values, different date
formats can be used. A date value should be enclosed in single quotes, like a string. When the
data type is datetime, SQL will convert the string to a datetime value.
Formats accepted are:
Page 4 of 11
CCI – Computer Science Dept 655445337.doc
Note that the default date format in SQL Server is to put the month before the date e.g.
12/31/03 – this is the US format. This is different to the European date format which puts the
date before the month e.g. 31/12/03. The date format depends also on the Regional settings
on the server.
However, you can specify the date format for SQL to use when converting strings to dates,
using the following SQL:
SET dateformat 'dmy' -- sets the format to be date then month
Select datepart (m, '6/2/04') -- should return 2 as 2 is the month part
Or
SET dateformat 'mdy' -- sets the format to be month then date
Select datepart (m, '6/2/04') -- should return 6 as 6 is now the month part
Be aware of this when working with dates as, for example, you could intend to enter a date of
May 3rd, 2003. If you enter it as '3/5/03' it could be interpreted by SQL Server as being March
5th, 2003. It should be entered as '5/3/03'.
5 Views
5.1 Definition
A SQL view is a virtual table – the contents of the virtual table are defined by a query. A
view consists of a set of named columns and rows of data – like a table. It can be treated like
a table e.g. it can be selected from in a query.
However, unlike a table, a view does not exist as a stored set of data values in the database.
The rows and columns of data come from the tables referenced in the query that defines the
view and are produced dynamically – only when the view itself is referenced.
In other words, what is stored in the database is the definition of the view – the SELECT
query that defines it. The result set of that SELECT statement forms the content of the virtual
table that is returned by the view when it is referenced e.g. in the FROM clause of a SELECT
query.
To join columns from multiple tables so that they appear like a single table
To aggregate information instead of supplying details e.g. to show only the SUM,
MIN or MAX of a particular column.
Data in the tables referenced by the defining query can usually be updated by updating the
view.
Page 5 of 11
CCI – Computer Science Dept 655445337.doc
A view can be created using the Create View statement. The syntax is as follows:
Take, for example, the titles and publishers table in the pubs demo database on SQL Server.
It is likely that a listing of titles together with publishers is frequently required.
This listing can be obtained by joining the two tables as follows:
To create a view based on this query, we use the CREATE VIEW SQL statement.
When this statement is executed, it does not return the rows from titles and pubs – it creates a
view object in the database.
This view can now be used as a table e.g. rows can be selected from it:
Generally, all the parts of the SELECT statement can be used when creating a view, but the
ORDER BY clause cannot be used. If it is necessary to order the results, they can be ordered
when selecting from the view e.g.
Modify a View
Page 6 of 11
CCI – Computer Science Dept 655445337.doc
To see the definition for a view, double-click on it. This brings up the View Properties dialog.
In this window, you can see the SQL that creates the view, including the Create View
statement.
To modify the query that defines the view, edit the SQL here.
Click the Check Syntax button after making a change – if there are any errors in the
SQL, they will be reported when the button is clicked.
Click Apply to apply the changes to the view – after applying, you can switch to the
Query Analyzer to check that the view contents are as you expect.
Click OK to apply the changes to the view and to exit from the dialog.
You can also modify a view in the Design View – right-click on the view and choose Design
View. This opens up the Query Designer – see the following section, Create a New View, for
more information about using the Query Designer.
The query designer window, being used to create the view vwTitlesPubs for which the SQL
was provided above, is shown in Figure 1 below.
Page 7 of 11
CCI – Computer Science Dept 655445337.doc
Figure 1 – creating a new view in Enterprise Manager – the query designer window
To execute the query and see the results, click the Run button in the toolbar – this is the red
exclamation mark icon (!). When the query is run, the results are shown in the bottom panel
for the window – see Figure 2 below.
Use this to check that the query is as you expect it to be. If it is necessary to make changes,
do so and run again to check the results.
Page 8 of 11
CCI – Computer Science Dept 655445337.doc
Figure 2 – creating a view in Enterprise Manager – the results are shown in the bottom panel after
clicking the Run button in the toolbar
When you are happy with the query, click the Save icon in the toolbar to create the
view.
You will be prompted to provide a name for the view – enter a name and click OK to
save.
When finished working with the view, close the query designer window. The new
view should now appear in the list of views in the database.
Drop a view
To drop a view in Enterprise Manager, navigate to the Views list in the database.
Click on the view to be dropped, and hit the Delete key or right-click and choose
Delete.
This will bring up the Drop Objects dialog.
To see if there are any objects dependent on the view and what objects it is dependent
on, click the Show Dependencies button.
Make sure that only the view you want to drop is listed, then click Drop All to drop it.
If there are any objects that depend on the view e.g. another view or a stored
procedure that references it, the view cannot be dropped until those objects are
dropped. SQL Server will warn if this is the case and will not drop the view.
Page 9 of 11
CCI – Computer Science Dept 655445337.doc
To change the current database, type the 'use' statement followed by the name of the
database into which you want to change e.g.
-- the next line of SQL will switch into the pubs database
use pubs
To signal the end of a batch of SQL statements – 'GO'. At this point, the batch will be sent to
the server for execution. GO is not a SQL statement (and is not part of the SQL92/99
standards) but is recognized by the Query Analyzer program. The current batch is all
statements entered since the last GO or since the start of the block of SQL statements.
If you save a set of SQL scripts for creating tables in a database and then inserting data into
those tables, a GO can be placed after the table creation statements. This means the tables
will be created before the data is inserted. For example:
Use my_database_name
GO
Create table mytable (
Column1 int not null,
Column 2 int
)
GO
Insert into mytable (Column1, Column2) values (1,2)
GO
To indicate that a single quote is part of a string and not the delimiter for the string, replace
the quote with two single quotes. For example, if inserting the last name "O'Sullivan" into a
database field, the apostrophe before the S is a single quote. If the string O'Sullivan is
delimited with single quotes for an insert query, an error like the following will be returned:
Page 11 of 11