+ All Categories
Home > Education > Sql server ___________session 2(sql 2008)

Sql server ___________session 2(sql 2008)

Date post: 12-Apr-2017
Category:
Upload: ehtisham-ali
View: 336 times
Download: 5 times
Share this document with a friend
47
Transact-SQL Language 2
Transcript
Page 1: Sql server  ___________session 2(sql 2008)

Transact-SQL Language

2

Page 2: Sql server  ___________session 2(sql 2008)

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.

Page 3: Sql server  ___________session 2(sql 2008)

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

Page 4: Sql server  ___________session 2(sql 2008)

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'

Page 5: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 5

Variable Declaration 2-2

--Variable declaration of type intDECLARE @age int = 40--Variable declaration of type decimalDECLARE @taxPercent decimal = 0.75--Variable declaration of type moneyDECLARE @amount money = 50000DECLARE @bonus money = @amount*.10--Variable declaration of type dateDECLARE @userDate date = '09-30-2009'DECLARE @currentDate date = GETDATE()SELECT @age, @taxPercent, @amount, @bonus,@userDate, @currentDate

ExampleThe following example shows variable declaration and initialization for different data types.

Output

Page 6: Sql server  ___________session 2(sql 2008)

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

Page 7: Sql server  ___________session 2(sql 2008)

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.

Page 8: Sql server  ___________session 2(sql 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-08Displays the admission date of a student in a university

Page 9: Sql server  ___________session 2(sql 2008)

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

Page 10: Sql server  ___________session 2(sql 2008)

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.

Page 11: Sql server  ___________session 2(sql 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) 5time(0) (8, 0) 3time(1) (10, 1) 3time(2) (11, 2) 3time(3) (12, 3) 4time(4) (13, 4) 4time(5) (14, 5) 5time(6) (15, 6) 5time(7) (16, 7) 5

Page 12: Sql server  ___________session 2(sql 2008)

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 variablefractional seconds precision: specifies the number of digits ranging from 0 to 7 for thefractional part of the secondsvalue: represents the value assigned to the variable

Page 13: Sql server  ___________session 2(sql 2008)

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;

SELECT @startTime8;

Output

10:10:30.123456710:10:3010:10:30.110:10:30.1210:10:30.12310:10:30.123510:10:30.1234610:10:30.12345710:10:30.1234567

Page 14: Sql server  ___________session 2(sql 2008)

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.

Page 15: Sql server  ___________session 2(sql 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) 8datetime2(0) (19, 0) 6datetime2(1) (21, 1) 6datetime2(2) (22, 2) 6datetime2(3) (23, 3) 7datetime2(4) (24, 4) 7datetime2(5) (25, 5) 8datetime2(6) (26, 6) 8datetime2(7) (27, 7) 8

Page 16: Sql server  ___________session 2(sql 2008)

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 variablefractional seconds precision: specifies the number of digits ranging from 0 to 7 for the fractional part of the secondsvalue: represents the value assigned to the variable

Page 17: Sql server  ___________session 2(sql 2008)

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.12345672009-09-08 11:59:112009-09-08 11:59:11.12009-09-08 11:59:11.122009-09-08 11:59:11.1232009-09-08 11:59:11.12352009-09-08 11:59:11.123462009-09-08 11:59:11.1234572009-09-08 11:59:11.1234567

Page 18: Sql server  ___________session 2(sql 2008)

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.

Page 19: Sql server  ___________session 2(sql 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

Page 20: Sql server  ___________session 2(sql 2008)

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 variablefractional seconds precision: specifies the number of digits ranging from 0 to 7 for the fractional part of the secondsvalue: represents the value assigned to the variable

Page 21: Sql server  ___________session 2(sql 2008)

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:002009-09-08 06:59:11 +00:002009-09-08 06:59:11.1 +00:002009-09-08 06:59:11.12 +00:002009-09-08 06:59:11.123 +00:002009-09-08 06:59:11.1235 +00:002009-09-08 06:59:11.12346 +00:002009-09-08 06:59:11.123457 +00:002009-09-08 06:59:11.1234567 +00:00

Page 22: Sql server  ___________session 2(sql 2008)

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

Page 23: Sql server  ___________session 2(sql 2008)

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)

Page 24: Sql server  ___________session 2(sql 2008)

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

Page 25: Sql server  ___________session 2(sql 2008)

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

Page 26: Sql server  ___________session 2(sql 2008)

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

Page 27: Sql server  ___________session 2(sql 2008)

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.

Page 28: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 28

Spatial Data Objects

Object DescriptionPoint A zero-dimensional object that represents a single

locationMultiPoint A series of zero or more points that are grouped

togetherLineString 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 ringMultiPolygon A set of one or more geometry or geography

Polygon instancesGeometryCollection A set of zero or more geometry instances

The geometry and geography data types have 11 spatial data objects, also called as instance types.

Page 29: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 29

Point

Example

-- Creating a Point geometryDECLARE @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

Page 30: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 30

MultiPoint

Example

-- Creating a MultiPoint geometryDECLARE @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

Page 31: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 31

LineString

Example

-- Creating a LineString geometryDECLARE @line geometry;SET @line = geometry::Parse('LINESTRING(1 1, 4 5, 10 13, 19 25)');SELECT @lineIn 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

Page 32: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 32

MultiLineString

Example

-- Creating a MultiPolygon geometryDECLARE @multipoly geometrySET @multipoly = geometry::Parse('MultiLineString ((0 0, 0 2, 2 3, 3 0, 0 0),(1 1, 1 2, 2 1, 1 1))');SELECT @multipolyIn 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

Page 33: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 33

Polygon

Example

-- Creating a Polygon geometryDECLARE @poly geometrySET @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

Page 34: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 34

MultiPolygon

Example

-- Creating a MultiPolygon geometryDECLARE @multipoly geometrySET @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

Page 35: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 35

GeometryCollection

Example

-- Creating a GeometryCollection geometryDECLARE @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

Page 36: Sql server  ___________session 2(sql 2008)

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_measure4120 metre4121 metre4122 metre4123 metre4124 metre

Page 37: Sql server  ___________session 2(sql 2008)

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

Page 38: Sql server  ___________session 2(sql 2008)

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 geometrySET @shapepoly = geometry::STGeomFromText ('POLYGON((0 2, 10 3, 3 4, 0 2))',4326);SELECT @shapepoly

Example

Output

Page 39: Sql server  ___________session 2(sql 2008)

New Features of SQL Server 2008 / Session 2 39

CONVERT() Function 1-2

Conversion process in SQL Server 2005Conversion process in SQL Server 2008

The CONVERT() function in SQL Server 2008 converts binary data into character data.

Page 40: Sql server  ___________session 2(sql 2008)

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 varcharStyle “1”: converts binary to varchar but the values stay the sameStyle “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

Page 41: Sql server  ___________session 2(sql 2008)

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

Page 42: Sql server  ___________session 2(sql 2008)

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

2007261521226121030123123456123456700022

Page 43: Sql server  ___________session 2(sql 2008)

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

Page 44: Sql server  ___________session 2(sql 2008)

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

20072June152122Friday121030123123456123456700+00:0022

Page 45: Sql server  ___________session 2(sql 2008)

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

Page 46: Sql server  ___________session 2(sql 2008)

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.0010.0020.0010.000.00

Page 47: Sql server  ___________session 2(sql 2008)

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.


Recommended