SlideShare a Scribd company logo
1 of 47
Transact-SQL
Language
2
New Features of SQL Server 2008 / Session 2 2
Objectives

Describe the process of declaring variables.

Explain the new data types in SQL Server 2008.

Describe sparse columns.

Explain the concept of wide tables.

Describe spatial data types.

Explain the hierarchyid data type.

Explain the new enhancements in the CONVERT()
function.

Describe the enhancements to date functions.

Explain the new XQuery functions.

Describe compound assignment operators.
New Features of SQL Server 2008 / Session 2 3
Variables and Data Types

Variable represents a location in computer
memory to store data

Data can be of different data types such as
integer, decimal, date, and so on

Declaration and initialization of variables were
done separately in SQL Server 2005
New Features of SQL Server 2008 / Session 2 4
Variable Declaration 1-2

SQL Server 2005
DECLARE @firstname varchar(25)
SET @firstname = 'harry'
Example

SQL Server 2008
Syntax
DECLARE @local_variable [AS] data_type[ = value ]
Example
DECLARE @firstname varchar(25) = 'harry'
New Features of SQL Server 2008 / Session 2 5
Variable Declaration 2-2
--Variable declaration of type int
DECLARE @age int = 40
--Variable declaration of type decimal
DECLARE @taxPercent decimal = 0.75
--Variable declaration of type money
DECLARE @amount money = 50000
DECLARE @bonus money = @amount*.10
--Variable declaration of type date
DECLARE @userDate date = '09-30-2009'
DECLARE @currentDate date = GETDATE()
SELECT @age, @taxPercent, @amount, @bonus,@userDate, @currentDate
Example
The following example shows variable declaration and initialization
for different data types.
Output
New Features of SQL Server 2008 / Session 2 6
New Date and Time Types

datetime and smalldatetime data types stored
the date with time component in SQL Server 2005

In SQL Server 2008, Microsoft introduced new date and
time data types

date

time
 datetme2

datetimeoffset

Allows to store not only a date, but have more precision
for the time component

Provides date and time related support to applications
deployed over the globe
New Features of SQL Server 2008 / Session 2 7
“date” Data Type 1-3

Stores only the date without storing the time

Range is January 1, 0001 to December 31, 9999

By default, the date is displayed in YYYY-MM-DD format

Occupies 3 bytes, and has a precision of 10 digits
The image shows selecting the date data type in SQL Server 2008.
New Features of SQL Server 2008 / Session 2 8
“date” Data Type 2-3
Syntax
DECLARE @local_variable [AS] data_type[ = value ]
Example
DECLARE @admissionDate date = '09-08-2009';
SELECT @admissionDate;
Output
2009-09-08
Displays the admission date of a student in a university
New Features of SQL Server 2008 / Session 2 9
“date” Data Type 3-3
DECLARE @admissionDate date = GETDATE()
SELECT @admissionDate
Output
Example
2009-08-15
DECLARE @admissionDate date = '09-08-2009
10:30:25';
SELECT @admissionDate;
Output
Example
2009-09-08
Returns the current date
Stores only the date component
New Features of SQL Server 2008 / Session 2 10
“time” Data Type 1-4

Stores only the time component

The time(7) data type from the Data Type dropdown seen in
Design view can be defined in a column

The precision is specified as time(n), where n represents the
precision value and should be in the range 0 to 7
The image shows selecting the time data type in SQL Server 2008.
New Features of SQL Server 2008 / Session 2 11
“time” Data Type 2-4

Supports up to 100 nanoseconds of accuracy

Range is from 00:00:00.0000000 to 23:59:59.9999999 and
default precision is 7 digits

Truncates the remaining decimal values having precisions more
than 7 digits
The following table summarizes the result in the precision value, and its
corresponding storage size, based on the different integer values passed as
a parameter to the time data type.
Scale Result (precision,
scale)
Storage
(bytes)
time (16, 7) 5
time(0) (8, 0) 3
time(1) (10, 1) 3
time(2) (11, 2) 3
time(3) (12, 3) 4
time(4) (13, 4) 4
time(5) (14, 5) 5
time(6) (15, 6) 5
time(7) (16, 7) 5
New Features of SQL Server 2008 / Session 2 12
“time” Data Type 3-4
Syntax
DECLARE @local_variable time [(fractional seconds
precision)] [ = value ]
where,
@local_variable: represents the name of a variable
fractional seconds precision: specifies the
number of digits ranging from 0 to 7 for the
fractional part of the seconds
value: represents the value assigned to the variable
New Features of SQL Server 2008 / Session 2 13
“time” Data Type 4-4
The following example shows all the possible declarations using the time data type.
DECLARE @startTime time = '10:10:30.1234567';
DECLARE @startTime1 time(0) = '10:10:30.1234567';
DECLARE @startTime2 time(1) = '10:10:30.1234567';
DECLARE @startTime3 time(2) = '10:10:30.1234567';
DECLARE @startTime4 time(3) = '10:10:30.1234567';
DECLARE @startTime5 time(4) = '10:10:30.1234567';
DECLARE @startTime6 time(5) = '10:10:30.1234567';
DECLARE @startTime7 time(6) = '10:10:30.1234567';
DECLARE @startTime8 time(7) = '10:10:30.1234567';
Example
SELECT @startTime;
SELECT @startTime1;
SELECT @startTime2;
SELECT @startTime3;
SELECT @startTime4;
SELECT @startTime5;
SELECT @startTime6;
SELECT @startTime7;
Output
10:10:30.1234567
10:10:30
10:10:30.1
10:10:30.12
10:10:30.123
10:10:30.1235
10:10:30.12346
10:10:30.123457
10:10:30.1234567
New Features of SQL Server 2008 / Session 2 14
“datetime2” Data Type 1-4

Is an enhancement to datetime

The datetime2(7) data type from the Data Type dropdown
seen in Design view can be defined in a column

Range is from January 1, 0001 to December 31, 9999

The default precision of datetime2 data type is 7 digits

Truncates the remaining decimal values having precision more
than 7 digits

Stores and displays date and time together in varying lengths
from 19 (YYYY-MM-DD hh:mm:ss) to 27 (YYYY-MM-DD
hh:mm:ss.0000000) characters in length
The image shows selecting the datetime2 data type in SQL
Server 2008.
New Features of SQL Server 2008 / Session 2 15
“datetime2” Data Type 2-4
The following table summarizes the result in the precision value, and its
corresponding storage size, based on the different integer values passed as
a parameter to the datetime2 data type.
Scale Result
(precision, scale)
Storage
(bytes)
datetime2 (27, 7) 8
datetime2(0) (19, 0) 6
datetime2(1) (21, 1) 6
datetime2(2) (22, 2) 6
datetime2(3) (23, 3) 7
datetime2(4) (24, 4) 7
datetime2(5) (25, 5) 8
datetime2(6) (26, 6) 8
datetime2(7) (27, 7) 8
New Features of SQL Server 2008 / Session 2 16
“datetime2” Data Type 3-4
Syntax
DECLARE @local_variable datetime2
[(fractional seconds precision)] [ = value ]
where,
@local_variable: represents the name of a variable
fractional seconds precision: specifies the number of
digits ranging from 0 to 7 for the fractional part of the seconds
value: represents the value assigned to the variable
New Features of SQL Server 2008 / Session 2 17
“datetime2” Data Type 4-4
The following example shows all the possible declarations in the datetime2 data
type.
DECLARE @bookingTime datetime2 = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime1 datetime2(0) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime2 datetime2(1) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime3 datetime2(2) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime4 datetime2(3) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime5 datetime2(4) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime6 datetime2(5) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime7 datetime2(6) = '2009-09-08 11:59:11.1234567';
DECLARE @bookingTime8 datetime2(7) = '2009-09-08
11:59:11.1234567';15.5
Example
SELECT @bookingTime;
SELECT @bookingTime1;
SELECT @bookingTime2;
SELECT @bookingTime3;
SELECT @bookingTime4;
SELECT @bookingTime5;
SELECT @bookingTime6;
SELECT @bookingTime7;
SELECT @bookingTime8;
Output
2009-09-08 11:59:11.1234567
2009-09-08 11:59:11
2009-09-08 11:59:11.1
2009-09-08 11:59:11.12
2009-09-08 11:59:11.123
2009-09-08 11:59:11.1235
2009-09-08 11:59:11.12346
2009-09-08 11:59:11.123457
2009-09-08 11:59:11.1234567
New Features of SQL Server 2008 / Session 2 18
“datetimeoffset” Data Type 1-4

Is an extension to the datetime2 data type

Allows you to store a date and time (based on 24-hour clock)
that is time zone aware

The time zone offset is indicated as + or - hh:mm, where hh
represents the hours and mm represents the minutes

The valid time zone range is from -14:00 through +14:00

The default precision of datetimeoffset data type is 7
digits, and the time portion has an accuracy upto 100
nanoseconds
The image shows selecting the datetimeoffset data type in SQL
Server 2008.
New Features of SQL Server 2008 / Session 2 19
“datetimeoffset” Data Type 2-4
The following table summarizes the result in the precision value, and its
corresponding storage size, based on the different integer values passed as
a parameter to the datetimeoffset data type.
Scale Result
(precision, scale)
Storage
(bytes)
datetimeoffset (34, 7) 10
datetimeoffset(0) (26, 0) 8
datetimeoffset(1) (28, 1) 8
datetimeoffset(0) (29, 2) 8
datetimeoffset(0) (30, 3) 9
datetimeoffset(0) (31, 4) 9
datetimeoffset(0) (32, 5) 10
datetimeoffset(0) (33, 6) 10
datetimeoffset(0) (34, 7) 10
New Features of SQL Server 2008 / Session 2 20
“datetimeoffset” Data Type 3-4
Syntax
DECLARE @local_variable datetimeoffset [(fractional
seconds precision)] [ = value ]
where,
@local_variable: represents the name of a variable
fractional seconds precision: specifies the number
of digits ranging from 0 to 7 for the fractional part of the
seconds
value: represents the value assigned to the variable
New Features of SQL Server 2008 / Session 2 21
“datetimeoffset” Data Type 4-4
The following example shows all the possible declarations in the datetimeoffset data
type.
DECLARE @sunriseTime datetimeoffset = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime1 datetimeoffset(0) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime2 datetimeoffset(1) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime3 datetimeoffset(2) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime4 datetimeoffset(3) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime5 datetimeoffset(4) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime6 datetimeoffset(5) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime7 datetimeoffset(6) = '2009-09-08 06:59:11.1234567';
DECLARE @sunriseTime8 datetimeoffset(7) = '2009-09-08 06:59:11.1234567';
Example
SELECT @sunriseTime;
SELECT @sunriseTime1;
SELECT @sunriseTime2;
SELECT @sunriseTime3;
SELECT @sunriseTime4;
SELECT @sunriseTime5;
SELECT @sunriseTime6;
SELECT @sunriseTime7;
SELECT @sunriseTime8;
Output
2009-09-08 06:59:11.1234567 +00:00
2009-09-08 06:59:11 +00:00
2009-09-08 06:59:11.1 +00:00
2009-09-08 06:59:11.12 +00:00
2009-09-08 06:59:11.123 +00:00
2009-09-08 06:59:11.1235 +00:00
2009-09-08 06:59:11.12346 +00:00
2009-09-08 06:59:11.123457 +00:00
2009-09-08 06:59:11.1234567 +00:00
New Features of SQL Server 2008 / Session 2 22
SPARSE Columns 1-2

An ordinary column that reduces the storage space occupied by a
column having null values

Use the SPARSE keyword to declare a column

Data types such as geography, geometry, image, ntext,
text, and timestamp cannot be marked as SPARSE

SPARSE columns can also be used with column sets and filtered
indexes

A column set is an additional column representing all the sparse
columns added to the table as an xml-typed column

Filtered indexes can be used to display only the rows that have
populated values in order to create a smaller and more efficient
index
Syntax
CREATE TABLE TABLENAME (ColumnName datatype SPARSE)
where,
SPARSE: indicates that it is a sparse column
New Features of SQL Server 2008 / Session 2 23
SPARSE Columns 2-2
The following example creates a table named Customers with five columns.
Among the five columns, the columns, ContactNumber and Comments, are
set as sparse columns using the SPARSE keyword. By marking the columns as
sparse, no space will be allocated for null values stored in these columns.
CREATE TABLE Customers
(
FirstName varchar(25) NOT NULL,
LastName varchar(25) NOT NULL,
ContactNumber varchar(15) SPARSE NULL,
Salary money NOT NULL,
Comments varchar(1000) SPARSE NULL
)
Example
The following example shows the creation of a table with sparse columns and
a column set.
CREATE TABLE Customers
(
FirstName varchar(25) NOT NULL,
LastName varchar(25) NOT NULL,
ContactNumber varchar(15) SPARSE NULL,
Salary money NOT NULL,
Comments varchar(1000) SPARSE NULL,
AllSparseColumns xml COLUMN_SET FOR
ALL_SPARSE_COLUMNS
)
New Features of SQL Server 2008 / Session 2 24
Wide Tables

In SQL Server, a table can have a maximum of 1024 columns

A wide table, a new special table type in SQL Server 2008,
enables you to increase the total number of columns to
30,000 by making use of sparse columns and column sets

The number of indexes and statistics is also increased to
1,000 and 30,000 respectively
 To convert a table into a wide table, you add a column set to
the table definition
 Wide tables has unique performance considerations, such as
increased run-time and compile-time memory requirements
New Features of SQL Server 2008 / Session 2 25
Wide Tables - Disadvantages

Increased cost to maintain indexes on the table because
wide tables define up to 30,000 columns

When a column is added or removed from a wide table, the
SQL query for that particular column becomes invalid as it
needs to be rewritten

When data is added or removed from a wide table, the
performance gets affected
New Features of SQL Server 2008 / Session 2 26
Spatial Data Types

Spatial data represents information about the geographical
location and the shape of objects, primarily on the earth

Spatial data provides information that can be used in
different scenarios such as:

Analyzing regional, national, or international sales
trends

Deciding where to place a new store based on
proximity to customers and competition

Navigating to a destination using a Global Positioning
System (GPS) device

Allowing customers to track the delivery of a parcel

Assessing the impact of environment changes such as
identifying houses at risk of floods
New Features of SQL Server 2008 / Session 2 27
“geometry” and “geography” Data Types

geometry and geography are two spatial data types in
SQL Server 2008

These data types include properties and methods to perform
operations such as calculating distances between locations
 geometry data type is used to store data of a flat 2-D
surface having XY coordinates that represent points, lines,
and polygons in a two-dimensional space

The geography data type uses latitude and longitude
angles to find the points on the earth
The image shows selecting the geometry and geography data
types in SQL Server 2008.
New Features of SQL Server 2008 / Session 2 28
Spatial Data Objects
Object Description
Point A zero-dimensional object that represents a single
location
MultiPoint A series of zero or more points that are grouped
together
LineString A one-dimensional object that represents sequence
of points that are connected by line segments to
make a complete line
MultiLineString A set of one or more LineString instances.
Polygon A two-dimensional shape representing a ring
MultiPolygon A set of one or more geometry or geography
Polygon instances
GeometryCollection A set of zero or more geometry instances
The geometry and geography data types have 11 spatial
data objects, also called as instance types.
New Features of SQL Server 2008 / Session 2 29
Point
Example
-- Creating a Point geometry
DECLARE @spoint geometry;
SET @spoint = geometry::Parse('POINT(2 5)');
SELECT @spoint
In this example, a variable named @spoint is declared using
the geometry data type. Here, a point is plotted on the graph at
the coordinates (2, 5).
Output
New Features of SQL Server 2008 / Session 2 30
MultiPoint
Example
-- Creating a MultiPoint geometry
DECLARE @mpoint geometry;
SET @mpoint = geometry::Parse('MULTIPOINT((6
8), (5 7))');
SELECT @mpoint
In this example, a variable named @mpoint is declared using the
geometry data type. Here, two points are plotted on the graph
at the coordinates (6,8) and (5,7).
Output
New Features of SQL Server 2008 / Session 2 31
LineString
Example
-- Creating a LineString geometry
DECLARE @line geometry;
SET @line = geometry::Parse('LINESTRING(1 1, 4
5, 10 13, 19 25)');
SELECT @line
In this example, a variable named @line is declared using the
geometry data type. Here, a sequence of points is plotted on
the graph at the coordinates (1,1), (4,5), (10,13) and (19,25).
These sequence of points are connected by intermediate line
segments to make it a complete single line starting from (1,1)
till (19,25).
Output
New Features of SQL Server 2008 / Session 2 32
MultiLineString
Example
-- Creating a MultiPolygon geometry
DECLARE @multipoly geometry
SET @multipoly =
geometry::Parse('MultiLineString ((0 0, 0 2, 2
3, 3 0, 0 0),
(1 1, 1 2, 2 1, 1 1))');
SELECT @multipoly
In this example, a variable named @multiline is declared using
the geometry data type. Here, two sets of sequence of points are
plotted on the graph. The first set has the coordinates (0,2), (1,1),
and the second set has the coordinates (1,2), (2,1), (2,2). Each set
of these sequence of points are connected to make a single line.
Output
New Features of SQL Server 2008 / Session 2 33
Polygon
Example
-- Creating a Polygon geometry
DECLARE @poly geometry
SET @poly = geometry::Parse('POLYGON((0 2, 10
3, 3 4, 0 2))');
SELECT @poly
In this example, a variable named @poly is declared using the
geometry data type. Here, a sequence of points is plotted on the
graph with the coordinates (0,1), (1,5), (5,5), (5,1), and (0,1). Note
that the first and the last coordinate values are the same to make a
ring formation, in order to create a polygon.
Output
New Features of SQL Server 2008 / Session 2 34
MultiPolygon
Example
-- Creating a MultiPolygon geometry
DECLARE @multipoly geometry
SET @multipoly = geometry::Parse('MultiPolygon
(((0 0, 0 2, 2 3, 3 0, 0 0),
(1 1, 1 2, 2 1, 1 1)))');
SELECT @multipoly
In this example, a variable named @multipoly is declared using
the geometry data type. Here, two set of sequence of points are
plotted on the graph to create two polygons. The output displayed
will show one polygon placed inside another polygon.
Output
New Features of SQL Server 2008 / Session 2 35
GeometryCollection
Example
-- Creating a GeometryCollection geometry
DECLARE @geomcollection geometry;
SET @geomcollection =
geometry::Parse('GEOMETRYCOLLECTION(POINT(4
4), LINESTRING(3 3, 8 3), POLYGON((0 2, 13 3,
4 5, 0 2)))');
SELECT @geomcollection
In this example, a variable named @geomcollection is declared
using the geometry data type. Here, three different geometric
shapes, a point, a line, and a polygon are displayed simultaneously.
Output
New Features of SQL Server 2008 / Session 2 36
Spatial Reference Identifiers (SRIDs)

Each spatial instance has an associated SRID

An SRID is a unique id used to determine a coordinate
system that enables to uniquely identify a specific position
on earth

SQL Server Database Engine uses SRID as 0 for geometry
data type, which is the default one

In geography data type, each position on earth is assigned
a specific SRID

The SRIDs can be viewed from a special table called
sys.spatial_reference_systems
The following code demonstrates how to retrieve data from the sys.spatial_reference_systems table. This table may
have six columns and more than 300 rows. Here, only first five rows and two columns are retrieved.
SELECT TOP 5 spatial_reference_id, unit_of_measure
FROM sys.spatial_reference_systems
Example
Output
spatial_reference_id
unit_of_measure
4120 metre
4121 metre
4122 metre
4123 metre
4124 metre
New Features of SQL Server 2008 / Session 2 37
Static Methods 1-2
Method Description
STPointFromText Creates a Point instance
STMPointFromText Creates a MultiPoint instance
STLineFromText Creates a LineString instance
STMLineFromText Creates a MultiLineString instance
STGeomCollFromText Creates a Geometry­Collection
instance
 You can insert new items of spatial data by using a static
method
 The most commonly used static method is
STGeomFromText()
 This method constructs a new instance from either a
geometry or geography data type
 The STGeomFromText() method has two parameters
 The first parameter represents the type of geometry or
geography instance that is to be created
 The second parameter represents the SRID of the instance
to be constructed
New Features of SQL Server 2008 / Session 2 38
Static Methods 2-2
Syntax
geometry::STGeomFromText('geometry_tagged_text', SRID)
where,
geometry_tagged_text: represents the shape of
geometry/geography instance to be constructed
SRID: is an int expression representing the position on the earth
In this example, a variable named @shapepoly is declared using the geometry data type. Here, a polygon is created with
the coordinates (0,2), (10,3), (3,4) and (0,2) using the STGeomFromText() method.
DECLARE @shapepoly geometry
SET @shapepoly = geometry::STGeomFromText
('POLYGON((0 2, 10 3, 3 4, 0 2))',4326);
SELECT @shapepoly
Example
Output
New Features of SQL Server 2008 / Session 2 39
CONVERT() Function 1-2
Conversion process in SQL Server 2005 Conversion process in SQL Server 2008

The CONVERT() function in SQL Server 2008 converts binary data into
character data.
New Features of SQL Server 2008 / Session 2 40
CONVERT() Function 2-2
CONVERT( data_type [( length ) ] , expression[, style ])
Syntax
SELECT CONVERT(varbinary(6),'aptech')
SELECT CONVERT(varchar(18), 0x617074656368, 0) AS 'Style 0'
SELECT CONVERT(varchar(18), 0x617074656368, 1) AS 'Style 1'
SELECT CONVERT(varchar(18), 0x617074656368, 2) AS 'Style 2'
The following example demonstrates the CONVERT() function to convert
character data to binary, and thereafter the binary result back into character
data.
Example
While converting character­to­binary / binary­to­character,
Style “0”: converts binary to varchar
Style “1”: converts binary to varchar but the values stay the same
Style “2”: strips the '0x' but leaves the rest of the values
where,
data_type: is the target data type (includes xml, bigint, and sql_variant)
length: An optional integer that specifies the length of the target data type
expression: is any valid expression
style: is an integer expression that specifies how the CONVERT() function
is to translate the expression. If style is NULL, NULL is returned
New Features of SQL Server 2008 / Session 2 41
Enhancements in “DATEPART()” 1-2

Retrieves values of individual components that
make up the date and time data types
 Introduces arguments microsecond, nanosecond, and
TZoffset, ISO_WEEK
where,
datepart: is the argument name whose date or time value is to be
extracted from the date expression
date: is an expression that evaluates to a value of type date, time,
smalldatetime, datetime, datetime2, or datetimeoffset
DATEPART ( datepart, date)
Syntax
New Features of SQL Server 2008 / Session 2 42
Enhancements in “DATEPART()” 2-2
SELECT DATEPART(YEAR, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(QUARTER, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(MONTH, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(DAYOFYEAR, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(DAY, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(WEEK, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(WEEKDAY, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(HOUR, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(MINUTE, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(SECOND, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(MILLISECOND, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(MICROSECOND, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(NANOSECOND, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(TZOFFSET, '2007-06-01 12:10:30.1234567');
SELECT DATEPART(ISO_WEEK, '2007-06-01 12:10:30.1234567');
Example Output
2007
26
152
1
22
6
12
10
30
123
123456
12345670
0
0
22
New Features of SQL Server 2008 / Session 2 43
Enhancements in “DATENAME()” 1-2

The DATENAME() function retrieves and returns the date and
time values in the form of character strings

The DATENAME() function supports the microsecond,
nanosecond, TZoffset and ISO_WEEK arguments
where,
datepart: is the argument name whose date or time value is
to be extracted from the date expression
date: is an expression that can be resolved to a date, time,
smalldatetime, datetime, datetime2, or
datetimeoffset value.
DATENAME ( datepart, date)
Syntax
New Features of SQL Server 2008 / Session 2 44
Enhancements in “DATENAME()” 2-2
SELECT DATENAME(YEAR, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(QUARTER, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(MONTH, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(DAYOFYEAR, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(DAY, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(WEEK, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(WEEKDAY, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(HOUR, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(MINUTE, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(SECOND, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(MILLISECOND, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(MICROSECOND, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(NANOSECOND, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(TZOFFSET, '2007-06-01 12:10:30.1234567');
SELECT DATENAME(ISO_WEEK, '2007-06-01 12:10:30.1234567');
Example Output
2007
2
June
152
1
22
Friday
12
10
30
123
123456
12345670
0
+00:00
22
New Features of SQL Server 2008 / Session 2 45
Compound Assignment Operators 1-2

SQL Server 2008 supports five compound assignment
operations:

Addition (+=)

Subtraction (­=)

Multiplication (*=)

Division (/=)

Modulus (%=)

The use of compound assignment operators result in faster
coding and compact code

Can be used in the SET clause of either the UPDATE or
SET statements or wherever assignment is normally
allowed
where,
operand1: is a user­defined variable
operand2: is a user­defined variable or a constant value
SET operand1 += operand2;
SET operand1 -= operand2;
SET operand1 *= operand2;
SET operand1 /= operand2;
SET operand1 %= operand2;
Syntax
New Features of SQL Server 2008 / Session 2 46
Compound Assignment Operators 2-2
DECLARE @value AS MONEY = 10.00;
SET @value += 2.00;
SELECT @value;
SET @value -= 2.00;
SELECT @value;
SET @value *= 2.00;
SELECT @value;
SET @value /= 2.00;
SELECT @value;
SET @value %= 2.00;
SELECT @value;
Example Output
12.00
10.00
20.00
10.00
0.00
New Features of SQL Server 2008 / Session 2 47
Summary

SQL Server 2008 has many enhancements in Transact
SQL statements including the new date and time data
types, sparse columns, wide tables, spatial data types,
and so on.

SQL Server 2008 has enhanced functions such as the
CONVERT() function, the DATE functions, and the
XQuery functions.

SQL Server 2008 has also introduced the compound
assignment operators such as +=, ­=, and so forth.

More Related Content

What's hot

Most useful queries
Most useful queriesMost useful queries
Most useful queriesSam Depp
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB plc
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)Buck Woolley
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
What’s New in MariaDB Server 10.2
What’s New in MariaDB Server 10.2What’s New in MariaDB Server 10.2
What’s New in MariaDB Server 10.2MariaDB plc
 
2 sql - single-row functions
2   sql - single-row functions2   sql - single-row functions
2 sql - single-row functionsAnkit Dubey
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1MariaDB plc
 

What's hot (15)

Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
What’s New in MariaDB Server 10.2
What’s New in MariaDB Server 10.2What’s New in MariaDB Server 10.2
What’s New in MariaDB Server 10.2
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
 
2 sql - single-row functions
2   sql - single-row functions2   sql - single-row functions
2 sql - single-row functions
 
Date and time functions in mysql
Date and time functions in mysqlDate and time functions in mysql
Date and time functions in mysql
 
Sq lite module8
Sq lite module8Sq lite module8
Sq lite module8
 
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
 
Sql analytic queries tips
Sql analytic queries tipsSql analytic queries tips
Sql analytic queries tips
 

Viewers also liked

Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sqlzahid6
 
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012ITCamp
 
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...Andrey Gershun
 
Alasql.js - SQL сервер на JavaScript
Alasql.js - SQL сервер на JavaScriptAlasql.js - SQL сервер на JavaScript
Alasql.js - SQL сервер на JavaScriptAndrey Gershun
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developersllangit
 
5 tsssisu sql_server_2012
5 tsssisu sql_server_20125 tsssisu sql_server_2012
5 tsssisu sql_server_2012Steve Xu
 
T sql語法之 cte 20140214
T sql語法之 cte 20140214T sql語法之 cte 20140214
T sql語法之 cte 20140214LearningTech
 
SQL Server 2008 for .NET Developers
SQL Server 2008 for .NET DevelopersSQL Server 2008 for .NET Developers
SQL Server 2008 for .NET Developersllangit
 
Transact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceTransact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceSteve Xu
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzationEhtisham Ali
 
High Performance Front-End Development
High Performance Front-End DevelopmentHigh Performance Front-End Development
High Performance Front-End Developmentdrywallbmb
 
X query language reference
X query language referenceX query language reference
X query language referenceSteve Xu
 
Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Andrey Gershun
 

Viewers also liked (20)

Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sql
 
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
 
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
 
Module03
Module03Module03
Module03
 
Module01
Module01Module01
Module01
 
Alasql.js - SQL сервер на JavaScript
Alasql.js - SQL сервер на JavaScriptAlasql.js - SQL сервер на JavaScript
Alasql.js - SQL сервер на JavaScript
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
Module07
Module07Module07
Module07
 
5 tsssisu sql_server_2012
5 tsssisu sql_server_20125 tsssisu sql_server_2012
5 tsssisu sql_server_2012
 
Module08
Module08Module08
Module08
 
T sql語法之 cte 20140214
T sql語法之 cte 20140214T sql語法之 cte 20140214
T sql語法之 cte 20140214
 
SQL Server 2008 for .NET Developers
SQL Server 2008 for .NET DevelopersSQL Server 2008 for .NET Developers
SQL Server 2008 for .NET Developers
 
Transact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceTransact sql data definition language - ddl- reference
Transact sql data definition language - ddl- reference
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
 
High Performance Front-End Development
High Performance Front-End DevelopmentHigh Performance Front-End Development
High Performance Front-End Development
 
Module02
Module02Module02
Module02
 
X query language reference
X query language referenceX query language reference
X query language reference
 
Module06
Module06Module06
Module06
 
Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)
 
Module05
Module05Module05
Module05
 

Similar to Sql server ___________session 2(sql 2008)

SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developersukdpe
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)Ehtisham Ali
 
Using New Data Types In2008
Using New Data Types In2008Using New Data Types In2008
Using New Data Types In2008PhilWinstanley
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle Abrar ali
 
New fordevelopersinsql server2008
New fordevelopersinsql server2008New fordevelopersinsql server2008
New fordevelopersinsql server2008Aaron Shilo
 
Mysql Fun
Mysql FunMysql Fun
Mysql FunSHC
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 OverviewEric Nelson
 
Sql server lesson5
Sql server lesson5Sql server lesson5
Sql server lesson5Ala Qunaibi
 
An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008Klaudiia Jacome
 
Les03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptLes03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptDrZeeshanBhatti
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Salman Memon
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)Ehtisham Ali
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmsqlserver content
 
MS SQL SERVER: Time series algorithm
MS SQL SERVER: Time series algorithmMS SQL SERVER: Time series algorithm
MS SQL SERVER: Time series algorithmDataminingTools Inc
 

Similar to Sql server ___________session 2(sql 2008) (20)

SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
Using New Data Types In2008
Using New Data Types In2008Using New Data Types In2008
Using New Data Types In2008
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
 
New fordevelopersinsql server2008
New fordevelopersinsql server2008New fordevelopersinsql server2008
New fordevelopersinsql server2008
 
Mysql Fun
Mysql FunMysql Fun
Mysql Fun
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
 
Sql server lesson5
Sql server lesson5Sql server lesson5
Sql server lesson5
 
An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008An introduction to new data warehouse scalability features in sql server 2008
An introduction to new data warehouse scalability features in sql server 2008
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
Les03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptLes03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.ppt
 
Sql
SqlSql
Sql
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
 
MS SQL SERVER: Time series algorithm
MS SQL SERVER: Time series algorithmMS SQL SERVER: Time series algorithm
MS SQL SERVER: Time series algorithm
 

More from Ehtisham Ali

Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)Ehtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modelingEhtisham Ali
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)Ehtisham Ali
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)Ehtisham Ali
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)Ehtisham Ali
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-introEhtisham Ali
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql serverEhtisham Ali
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control languageEhtisham Ali
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)Ehtisham Ali
 

More from Ehtisham Ali (14)

Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modeling
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-intro
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql server
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control language
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)
 

Recently uploaded

ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Sql server ___________session 2(sql 2008)

  • 2. New Features of SQL Server 2008 / Session 2 2 Objectives  Describe the process of declaring variables.  Explain the new data types in SQL Server 2008.  Describe sparse columns.  Explain the concept of wide tables.  Describe spatial data types.  Explain the hierarchyid data type.  Explain the new enhancements in the CONVERT() function.  Describe the enhancements to date functions.  Explain the new XQuery functions.  Describe compound assignment operators.
  • 3. New Features of SQL Server 2008 / Session 2 3 Variables and Data Types  Variable represents a location in computer memory to store data  Data can be of different data types such as integer, decimal, date, and so on  Declaration and initialization of variables were done separately in SQL Server 2005
  • 4. New Features of SQL Server 2008 / Session 2 4 Variable Declaration 1-2  SQL Server 2005 DECLARE @firstname varchar(25) SET @firstname = 'harry' Example  SQL Server 2008 Syntax DECLARE @local_variable [AS] data_type[ = value ] Example DECLARE @firstname varchar(25) = 'harry'
  • 5. New Features of SQL Server 2008 / Session 2 5 Variable Declaration 2-2 --Variable declaration of type int DECLARE @age int = 40 --Variable declaration of type decimal DECLARE @taxPercent decimal = 0.75 --Variable declaration of type money DECLARE @amount money = 50000 DECLARE @bonus money = @amount*.10 --Variable declaration of type date DECLARE @userDate date = '09-30-2009' DECLARE @currentDate date = GETDATE() SELECT @age, @taxPercent, @amount, @bonus,@userDate, @currentDate Example The following example shows variable declaration and initialization for different data types. Output
  • 6. New Features of SQL Server 2008 / Session 2 6 New Date and Time Types  datetime and smalldatetime data types stored the date with time component in SQL Server 2005  In SQL Server 2008, Microsoft introduced new date and time data types  date  time  datetme2  datetimeoffset  Allows to store not only a date, but have more precision for the time component  Provides date and time related support to applications deployed over the globe
  • 7. New Features of SQL Server 2008 / Session 2 7 “date” Data Type 1-3  Stores only the date without storing the time  Range is January 1, 0001 to December 31, 9999  By default, the date is displayed in YYYY-MM-DD format  Occupies 3 bytes, and has a precision of 10 digits The image shows selecting the date data type in SQL Server 2008.
  • 8. New Features of SQL Server 2008 / Session 2 8 “date” Data Type 2-3 Syntax DECLARE @local_variable [AS] data_type[ = value ] Example DECLARE @admissionDate date = '09-08-2009'; SELECT @admissionDate; Output 2009-09-08 Displays the admission date of a student in a university
  • 9. New Features of SQL Server 2008 / Session 2 9 “date” Data Type 3-3 DECLARE @admissionDate date = GETDATE() SELECT @admissionDate Output Example 2009-08-15 DECLARE @admissionDate date = '09-08-2009 10:30:25'; SELECT @admissionDate; Output Example 2009-09-08 Returns the current date Stores only the date component
  • 10. New Features of SQL Server 2008 / Session 2 10 “time” Data Type 1-4  Stores only the time component  The time(7) data type from the Data Type dropdown seen in Design view can be defined in a column  The precision is specified as time(n), where n represents the precision value and should be in the range 0 to 7 The image shows selecting the time data type in SQL Server 2008.
  • 11. New Features of SQL Server 2008 / Session 2 11 “time” Data Type 2-4  Supports up to 100 nanoseconds of accuracy  Range is from 00:00:00.0000000 to 23:59:59.9999999 and default precision is 7 digits  Truncates the remaining decimal values having precisions more than 7 digits The following table summarizes the result in the precision value, and its corresponding storage size, based on the different integer values passed as a parameter to the time data type. Scale Result (precision, scale) Storage (bytes) time (16, 7) 5 time(0) (8, 0) 3 time(1) (10, 1) 3 time(2) (11, 2) 3 time(3) (12, 3) 4 time(4) (13, 4) 4 time(5) (14, 5) 5 time(6) (15, 6) 5 time(7) (16, 7) 5
  • 12. New Features of SQL Server 2008 / Session 2 12 “time” Data Type 3-4 Syntax DECLARE @local_variable time [(fractional seconds precision)] [ = value ] where, @local_variable: represents the name of a variable fractional seconds precision: specifies the number of digits ranging from 0 to 7 for the fractional part of the seconds value: represents the value assigned to the variable
  • 13. New Features of SQL Server 2008 / Session 2 13 “time” Data Type 4-4 The following example shows all the possible declarations using the time data type. DECLARE @startTime time = '10:10:30.1234567'; DECLARE @startTime1 time(0) = '10:10:30.1234567'; DECLARE @startTime2 time(1) = '10:10:30.1234567'; DECLARE @startTime3 time(2) = '10:10:30.1234567'; DECLARE @startTime4 time(3) = '10:10:30.1234567'; DECLARE @startTime5 time(4) = '10:10:30.1234567'; DECLARE @startTime6 time(5) = '10:10:30.1234567'; DECLARE @startTime7 time(6) = '10:10:30.1234567'; DECLARE @startTime8 time(7) = '10:10:30.1234567'; Example SELECT @startTime; SELECT @startTime1; SELECT @startTime2; SELECT @startTime3; SELECT @startTime4; SELECT @startTime5; SELECT @startTime6; SELECT @startTime7; Output 10:10:30.1234567 10:10:30 10:10:30.1 10:10:30.12 10:10:30.123 10:10:30.1235 10:10:30.12346 10:10:30.123457 10:10:30.1234567
  • 14. New Features of SQL Server 2008 / Session 2 14 “datetime2” Data Type 1-4  Is an enhancement to datetime  The datetime2(7) data type from the Data Type dropdown seen in Design view can be defined in a column  Range is from January 1, 0001 to December 31, 9999  The default precision of datetime2 data type is 7 digits  Truncates the remaining decimal values having precision more than 7 digits  Stores and displays date and time together in varying lengths from 19 (YYYY-MM-DD hh:mm:ss) to 27 (YYYY-MM-DD hh:mm:ss.0000000) characters in length The image shows selecting the datetime2 data type in SQL Server 2008.
  • 15. New Features of SQL Server 2008 / Session 2 15 “datetime2” Data Type 2-4 The following table summarizes the result in the precision value, and its corresponding storage size, based on the different integer values passed as a parameter to the datetime2 data type. Scale Result (precision, scale) Storage (bytes) datetime2 (27, 7) 8 datetime2(0) (19, 0) 6 datetime2(1) (21, 1) 6 datetime2(2) (22, 2) 6 datetime2(3) (23, 3) 7 datetime2(4) (24, 4) 7 datetime2(5) (25, 5) 8 datetime2(6) (26, 6) 8 datetime2(7) (27, 7) 8
  • 16. New Features of SQL Server 2008 / Session 2 16 “datetime2” Data Type 3-4 Syntax DECLARE @local_variable datetime2 [(fractional seconds precision)] [ = value ] where, @local_variable: represents the name of a variable fractional seconds precision: specifies the number of digits ranging from 0 to 7 for the fractional part of the seconds value: represents the value assigned to the variable
  • 17. New Features of SQL Server 2008 / Session 2 17 “datetime2” Data Type 4-4 The following example shows all the possible declarations in the datetime2 data type. DECLARE @bookingTime datetime2 = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime1 datetime2(0) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime2 datetime2(1) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime3 datetime2(2) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime4 datetime2(3) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime5 datetime2(4) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime6 datetime2(5) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime7 datetime2(6) = '2009-09-08 11:59:11.1234567'; DECLARE @bookingTime8 datetime2(7) = '2009-09-08 11:59:11.1234567';15.5 Example SELECT @bookingTime; SELECT @bookingTime1; SELECT @bookingTime2; SELECT @bookingTime3; SELECT @bookingTime4; SELECT @bookingTime5; SELECT @bookingTime6; SELECT @bookingTime7; SELECT @bookingTime8; Output 2009-09-08 11:59:11.1234567 2009-09-08 11:59:11 2009-09-08 11:59:11.1 2009-09-08 11:59:11.12 2009-09-08 11:59:11.123 2009-09-08 11:59:11.1235 2009-09-08 11:59:11.12346 2009-09-08 11:59:11.123457 2009-09-08 11:59:11.1234567
  • 18. New Features of SQL Server 2008 / Session 2 18 “datetimeoffset” Data Type 1-4  Is an extension to the datetime2 data type  Allows you to store a date and time (based on 24-hour clock) that is time zone aware  The time zone offset is indicated as + or - hh:mm, where hh represents the hours and mm represents the minutes  The valid time zone range is from -14:00 through +14:00  The default precision of datetimeoffset data type is 7 digits, and the time portion has an accuracy upto 100 nanoseconds The image shows selecting the datetimeoffset data type in SQL Server 2008.
  • 19. New Features of SQL Server 2008 / Session 2 19 “datetimeoffset” Data Type 2-4 The following table summarizes the result in the precision value, and its corresponding storage size, based on the different integer values passed as a parameter to the datetimeoffset data type. Scale Result (precision, scale) Storage (bytes) datetimeoffset (34, 7) 10 datetimeoffset(0) (26, 0) 8 datetimeoffset(1) (28, 1) 8 datetimeoffset(0) (29, 2) 8 datetimeoffset(0) (30, 3) 9 datetimeoffset(0) (31, 4) 9 datetimeoffset(0) (32, 5) 10 datetimeoffset(0) (33, 6) 10 datetimeoffset(0) (34, 7) 10
  • 20. New Features of SQL Server 2008 / Session 2 20 “datetimeoffset” Data Type 3-4 Syntax DECLARE @local_variable datetimeoffset [(fractional seconds precision)] [ = value ] where, @local_variable: represents the name of a variable fractional seconds precision: specifies the number of digits ranging from 0 to 7 for the fractional part of the seconds value: represents the value assigned to the variable
  • 21. New Features of SQL Server 2008 / Session 2 21 “datetimeoffset” Data Type 4-4 The following example shows all the possible declarations in the datetimeoffset data type. DECLARE @sunriseTime datetimeoffset = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime1 datetimeoffset(0) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime2 datetimeoffset(1) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime3 datetimeoffset(2) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime4 datetimeoffset(3) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime5 datetimeoffset(4) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime6 datetimeoffset(5) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime7 datetimeoffset(6) = '2009-09-08 06:59:11.1234567'; DECLARE @sunriseTime8 datetimeoffset(7) = '2009-09-08 06:59:11.1234567'; Example SELECT @sunriseTime; SELECT @sunriseTime1; SELECT @sunriseTime2; SELECT @sunriseTime3; SELECT @sunriseTime4; SELECT @sunriseTime5; SELECT @sunriseTime6; SELECT @sunriseTime7; SELECT @sunriseTime8; Output 2009-09-08 06:59:11.1234567 +00:00 2009-09-08 06:59:11 +00:00 2009-09-08 06:59:11.1 +00:00 2009-09-08 06:59:11.12 +00:00 2009-09-08 06:59:11.123 +00:00 2009-09-08 06:59:11.1235 +00:00 2009-09-08 06:59:11.12346 +00:00 2009-09-08 06:59:11.123457 +00:00 2009-09-08 06:59:11.1234567 +00:00
  • 22. New Features of SQL Server 2008 / Session 2 22 SPARSE Columns 1-2  An ordinary column that reduces the storage space occupied by a column having null values  Use the SPARSE keyword to declare a column  Data types such as geography, geometry, image, ntext, text, and timestamp cannot be marked as SPARSE  SPARSE columns can also be used with column sets and filtered indexes  A column set is an additional column representing all the sparse columns added to the table as an xml-typed column  Filtered indexes can be used to display only the rows that have populated values in order to create a smaller and more efficient index Syntax CREATE TABLE TABLENAME (ColumnName datatype SPARSE) where, SPARSE: indicates that it is a sparse column
  • 23. New Features of SQL Server 2008 / Session 2 23 SPARSE Columns 2-2 The following example creates a table named Customers with five columns. Among the five columns, the columns, ContactNumber and Comments, are set as sparse columns using the SPARSE keyword. By marking the columns as sparse, no space will be allocated for null values stored in these columns. CREATE TABLE Customers ( FirstName varchar(25) NOT NULL, LastName varchar(25) NOT NULL, ContactNumber varchar(15) SPARSE NULL, Salary money NOT NULL, Comments varchar(1000) SPARSE NULL ) Example The following example shows the creation of a table with sparse columns and a column set. CREATE TABLE Customers ( FirstName varchar(25) NOT NULL, LastName varchar(25) NOT NULL, ContactNumber varchar(15) SPARSE NULL, Salary money NOT NULL, Comments varchar(1000) SPARSE NULL, AllSparseColumns xml COLUMN_SET FOR ALL_SPARSE_COLUMNS )
  • 24. New Features of SQL Server 2008 / Session 2 24 Wide Tables  In SQL Server, a table can have a maximum of 1024 columns  A wide table, a new special table type in SQL Server 2008, enables you to increase the total number of columns to 30,000 by making use of sparse columns and column sets  The number of indexes and statistics is also increased to 1,000 and 30,000 respectively  To convert a table into a wide table, you add a column set to the table definition  Wide tables has unique performance considerations, such as increased run-time and compile-time memory requirements
  • 25. New Features of SQL Server 2008 / Session 2 25 Wide Tables - Disadvantages  Increased cost to maintain indexes on the table because wide tables define up to 30,000 columns  When a column is added or removed from a wide table, the SQL query for that particular column becomes invalid as it needs to be rewritten  When data is added or removed from a wide table, the performance gets affected
  • 26. New Features of SQL Server 2008 / Session 2 26 Spatial Data Types  Spatial data represents information about the geographical location and the shape of objects, primarily on the earth  Spatial data provides information that can be used in different scenarios such as:  Analyzing regional, national, or international sales trends  Deciding where to place a new store based on proximity to customers and competition  Navigating to a destination using a Global Positioning System (GPS) device  Allowing customers to track the delivery of a parcel  Assessing the impact of environment changes such as identifying houses at risk of floods
  • 27. New Features of SQL Server 2008 / Session 2 27 “geometry” and “geography” Data Types  geometry and geography are two spatial data types in SQL Server 2008  These data types include properties and methods to perform operations such as calculating distances between locations  geometry data type is used to store data of a flat 2-D surface having XY coordinates that represent points, lines, and polygons in a two-dimensional space  The geography data type uses latitude and longitude angles to find the points on the earth The image shows selecting the geometry and geography data types in SQL Server 2008.
  • 28. New Features of SQL Server 2008 / Session 2 28 Spatial Data Objects Object Description Point A zero-dimensional object that represents a single location MultiPoint A series of zero or more points that are grouped together LineString A one-dimensional object that represents sequence of points that are connected by line segments to make a complete line MultiLineString A set of one or more LineString instances. Polygon A two-dimensional shape representing a ring MultiPolygon A set of one or more geometry or geography Polygon instances GeometryCollection A set of zero or more geometry instances The geometry and geography data types have 11 spatial data objects, also called as instance types.
  • 29. New Features of SQL Server 2008 / Session 2 29 Point Example -- Creating a Point geometry DECLARE @spoint geometry; SET @spoint = geometry::Parse('POINT(2 5)'); SELECT @spoint In this example, a variable named @spoint is declared using the geometry data type. Here, a point is plotted on the graph at the coordinates (2, 5). Output
  • 30. New Features of SQL Server 2008 / Session 2 30 MultiPoint Example -- Creating a MultiPoint geometry DECLARE @mpoint geometry; SET @mpoint = geometry::Parse('MULTIPOINT((6 8), (5 7))'); SELECT @mpoint In this example, a variable named @mpoint is declared using the geometry data type. Here, two points are plotted on the graph at the coordinates (6,8) and (5,7). Output
  • 31. New Features of SQL Server 2008 / Session 2 31 LineString Example -- Creating a LineString geometry DECLARE @line geometry; SET @line = geometry::Parse('LINESTRING(1 1, 4 5, 10 13, 19 25)'); SELECT @line In this example, a variable named @line is declared using the geometry data type. Here, a sequence of points is plotted on the graph at the coordinates (1,1), (4,5), (10,13) and (19,25). These sequence of points are connected by intermediate line segments to make it a complete single line starting from (1,1) till (19,25). Output
  • 32. New Features of SQL Server 2008 / Session 2 32 MultiLineString Example -- Creating a MultiPolygon geometry DECLARE @multipoly geometry SET @multipoly = geometry::Parse('MultiLineString ((0 0, 0 2, 2 3, 3 0, 0 0), (1 1, 1 2, 2 1, 1 1))'); SELECT @multipoly In this example, a variable named @multiline is declared using the geometry data type. Here, two sets of sequence of points are plotted on the graph. The first set has the coordinates (0,2), (1,1), and the second set has the coordinates (1,2), (2,1), (2,2). Each set of these sequence of points are connected to make a single line. Output
  • 33. New Features of SQL Server 2008 / Session 2 33 Polygon Example -- Creating a Polygon geometry DECLARE @poly geometry SET @poly = geometry::Parse('POLYGON((0 2, 10 3, 3 4, 0 2))'); SELECT @poly In this example, a variable named @poly is declared using the geometry data type. Here, a sequence of points is plotted on the graph with the coordinates (0,1), (1,5), (5,5), (5,1), and (0,1). Note that the first and the last coordinate values are the same to make a ring formation, in order to create a polygon. Output
  • 34. New Features of SQL Server 2008 / Session 2 34 MultiPolygon Example -- Creating a MultiPolygon geometry DECLARE @multipoly geometry SET @multipoly = geometry::Parse('MultiPolygon (((0 0, 0 2, 2 3, 3 0, 0 0), (1 1, 1 2, 2 1, 1 1)))'); SELECT @multipoly In this example, a variable named @multipoly is declared using the geometry data type. Here, two set of sequence of points are plotted on the graph to create two polygons. The output displayed will show one polygon placed inside another polygon. Output
  • 35. New Features of SQL Server 2008 / Session 2 35 GeometryCollection Example -- Creating a GeometryCollection geometry DECLARE @geomcollection geometry; SET @geomcollection = geometry::Parse('GEOMETRYCOLLECTION(POINT(4 4), LINESTRING(3 3, 8 3), POLYGON((0 2, 13 3, 4 5, 0 2)))'); SELECT @geomcollection In this example, a variable named @geomcollection is declared using the geometry data type. Here, three different geometric shapes, a point, a line, and a polygon are displayed simultaneously. Output
  • 36. New Features of SQL Server 2008 / Session 2 36 Spatial Reference Identifiers (SRIDs)  Each spatial instance has an associated SRID  An SRID is a unique id used to determine a coordinate system that enables to uniquely identify a specific position on earth  SQL Server Database Engine uses SRID as 0 for geometry data type, which is the default one  In geography data type, each position on earth is assigned a specific SRID  The SRIDs can be viewed from a special table called sys.spatial_reference_systems The following code demonstrates how to retrieve data from the sys.spatial_reference_systems table. This table may have six columns and more than 300 rows. Here, only first five rows and two columns are retrieved. SELECT TOP 5 spatial_reference_id, unit_of_measure FROM sys.spatial_reference_systems Example Output spatial_reference_id unit_of_measure 4120 metre 4121 metre 4122 metre 4123 metre 4124 metre
  • 37. New Features of SQL Server 2008 / Session 2 37 Static Methods 1-2 Method Description STPointFromText Creates a Point instance STMPointFromText Creates a MultiPoint instance STLineFromText Creates a LineString instance STMLineFromText Creates a MultiLineString instance STGeomCollFromText Creates a Geometry­Collection instance  You can insert new items of spatial data by using a static method  The most commonly used static method is STGeomFromText()  This method constructs a new instance from either a geometry or geography data type  The STGeomFromText() method has two parameters  The first parameter represents the type of geometry or geography instance that is to be created  The second parameter represents the SRID of the instance to be constructed
  • 38. New Features of SQL Server 2008 / Session 2 38 Static Methods 2-2 Syntax geometry::STGeomFromText('geometry_tagged_text', SRID) where, geometry_tagged_text: represents the shape of geometry/geography instance to be constructed SRID: is an int expression representing the position on the earth In this example, a variable named @shapepoly is declared using the geometry data type. Here, a polygon is created with the coordinates (0,2), (10,3), (3,4) and (0,2) using the STGeomFromText() method. DECLARE @shapepoly geometry SET @shapepoly = geometry::STGeomFromText ('POLYGON((0 2, 10 3, 3 4, 0 2))',4326); SELECT @shapepoly Example Output
  • 39. New Features of SQL Server 2008 / Session 2 39 CONVERT() Function 1-2 Conversion process in SQL Server 2005 Conversion process in SQL Server 2008  The CONVERT() function in SQL Server 2008 converts binary data into character data.
  • 40. New Features of SQL Server 2008 / Session 2 40 CONVERT() Function 2-2 CONVERT( data_type [( length ) ] , expression[, style ]) Syntax SELECT CONVERT(varbinary(6),'aptech') SELECT CONVERT(varchar(18), 0x617074656368, 0) AS 'Style 0' SELECT CONVERT(varchar(18), 0x617074656368, 1) AS 'Style 1' SELECT CONVERT(varchar(18), 0x617074656368, 2) AS 'Style 2' The following example demonstrates the CONVERT() function to convert character data to binary, and thereafter the binary result back into character data. Example While converting character­to­binary / binary­to­character, Style “0”: converts binary to varchar Style “1”: converts binary to varchar but the values stay the same Style “2”: strips the '0x' but leaves the rest of the values where, data_type: is the target data type (includes xml, bigint, and sql_variant) length: An optional integer that specifies the length of the target data type expression: is any valid expression style: is an integer expression that specifies how the CONVERT() function is to translate the expression. If style is NULL, NULL is returned
  • 41. New Features of SQL Server 2008 / Session 2 41 Enhancements in “DATEPART()” 1-2  Retrieves values of individual components that make up the date and time data types  Introduces arguments microsecond, nanosecond, and TZoffset, ISO_WEEK where, datepart: is the argument name whose date or time value is to be extracted from the date expression date: is an expression that evaluates to a value of type date, time, smalldatetime, datetime, datetime2, or datetimeoffset DATEPART ( datepart, date) Syntax
  • 42. New Features of SQL Server 2008 / Session 2 42 Enhancements in “DATEPART()” 2-2 SELECT DATEPART(YEAR, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(QUARTER, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(MONTH, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(DAYOFYEAR, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(DAY, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(WEEK, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(WEEKDAY, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(HOUR, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(MINUTE, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(SECOND, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(MILLISECOND, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(MICROSECOND, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(NANOSECOND, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(TZOFFSET, '2007-06-01 12:10:30.1234567'); SELECT DATEPART(ISO_WEEK, '2007-06-01 12:10:30.1234567'); Example Output 2007 26 152 1 22 6 12 10 30 123 123456 12345670 0 0 22
  • 43. New Features of SQL Server 2008 / Session 2 43 Enhancements in “DATENAME()” 1-2  The DATENAME() function retrieves and returns the date and time values in the form of character strings  The DATENAME() function supports the microsecond, nanosecond, TZoffset and ISO_WEEK arguments where, datepart: is the argument name whose date or time value is to be extracted from the date expression date: is an expression that can be resolved to a date, time, smalldatetime, datetime, datetime2, or datetimeoffset value. DATENAME ( datepart, date) Syntax
  • 44. New Features of SQL Server 2008 / Session 2 44 Enhancements in “DATENAME()” 2-2 SELECT DATENAME(YEAR, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(QUARTER, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(MONTH, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(DAYOFYEAR, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(DAY, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(WEEK, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(WEEKDAY, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(HOUR, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(MINUTE, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(SECOND, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(MILLISECOND, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(MICROSECOND, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(NANOSECOND, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(TZOFFSET, '2007-06-01 12:10:30.1234567'); SELECT DATENAME(ISO_WEEK, '2007-06-01 12:10:30.1234567'); Example Output 2007 2 June 152 1 22 Friday 12 10 30 123 123456 12345670 0 +00:00 22
  • 45. New Features of SQL Server 2008 / Session 2 45 Compound Assignment Operators 1-2  SQL Server 2008 supports five compound assignment operations:  Addition (+=)  Subtraction (­=)  Multiplication (*=)  Division (/=)  Modulus (%=)  The use of compound assignment operators result in faster coding and compact code  Can be used in the SET clause of either the UPDATE or SET statements or wherever assignment is normally allowed where, operand1: is a user­defined variable operand2: is a user­defined variable or a constant value SET operand1 += operand2; SET operand1 -= operand2; SET operand1 *= operand2; SET operand1 /= operand2; SET operand1 %= operand2; Syntax
  • 46. New Features of SQL Server 2008 / Session 2 46 Compound Assignment Operators 2-2 DECLARE @value AS MONEY = 10.00; SET @value += 2.00; SELECT @value; SET @value -= 2.00; SELECT @value; SET @value *= 2.00; SELECT @value; SET @value /= 2.00; SELECT @value; SET @value %= 2.00; SELECT @value; Example Output 12.00 10.00 20.00 10.00 0.00
  • 47. New Features of SQL Server 2008 / Session 2 47 Summary  SQL Server 2008 has many enhancements in Transact SQL statements including the new date and time data types, sparse columns, wide tables, spatial data types, and so on.  SQL Server 2008 has enhanced functions such as the CONVERT() function, the DATE functions, and the XQuery functions.  SQL Server 2008 has also introduced the compound assignment operators such as +=, ­=, and so forth.