Working With Data Types
Working With Data Types
Module Overview
Using Data Types
Working with Character Data
Converting Data Types
Working with Specialized Data Types
Constrain the type of data that an object can hold and the
permitted operations
Notes
tinyint
8 bits (0 to 255)
smallint
int
bigint
decimal
numeric
smallmoney
money
bit
values of 1, 0, or NULL
Data Type
Notes
float
real
Notes
date
datetime2
datetime
datetimeoffset
smalldatetime
time
Unique Identifiers
uniqueidentifier data type is typically used for storing
GUID values
=, <>, <, >, <=, >= are supported along with NULL and
CREATE
CREATE TABLE
TABLE Sales.Opportunity
Sales.Opportunity
(
(
OpportunityID
OpportunityID int NOT NULL,
Requirements
Requirements nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
ReceivedDate
ReceivedDate date
date NOT
NOT NULL,
NULL,
LikelyClosingDate
LikelyClosingDate date
date NULL,
NULL,
SalespersonID
SalespersonID int NULL,
Rating
Rating int
int NOT
NOT NULL
NULL
);
);
Unicode
Is a worldwide character-encoding standard
Simplifies software localization
Improves multilingual character processing
Is implemented in SQL Server as double-byte for Unicode types
Requires N prefix on constants
Uses LEN() to return number of characters, DATALENGTH() to
DECLARE
DECLARE @Hello
@Hello nvarchar(10);
nvarchar(10);
SET
SET @Hello
@Hello =
= N'Hello';
N'Hello';
SET
SET @Hello
@Hello =
= N'
N'
';
';
SET
SET @Hello
@Hello =
= N'
N'
';
';
Notes
char
nchar
varchar
nvarchar
varchar(max)
nvarchar(max)
text
ntext
Understanding Collations
Collations in SQL Server control:
Rules that govern how SQL Server sorts and compares values
for non-Unicode types
SELECT
SELECT *
FROM
FROM Production.Product
Production.Product
WHERE
WHERE Name LIKE
LIKE N'%ball%'
N'%ball%' COLLATE
COLLATE
SQL_Latin1_General_Cp1_CS_AS;
SQL_Latin1_General_Cp1_CS_AS;
sql_variant
Example:
Japanese_Bushu_Kakusu_100_CI_AS_SC
specifying a culture
SELECT
SELECT CAST(SYSDATETIME()
CAST(SYSDATETIME() AS nvarchar(30));
nvarchar(30));
SELECT
SELECT CONVERT(varchar(8),SYSDATETIME(),112);
CONVERT(varchar(8),SYSDATETIME(),112);
SELECT
SELECT CONVERT(char(8),
CONVERT(char(8), 0x4E616d65,
0x4E616d65, 0)
0)
AS
AS 'Style
'Style 0,
0, binary
binary to character';
character';
SELECT
SELECT PARSE('Monday,
PARSE('Monday, 13
13 December
December 2010'
2010' AS
AS
datetime2
datetime2 USING
USING 'en-US');
'en-US');
SELECT
SELECT
CASE
CASE WHEN
WHEN TRY_PARSE('NotLikely'
TRY_PARSE('NotLikely' AS decimal
decimal
USING
USING
'sr-Latn-CS')
'sr-Latn-CS') IS
IS NULL
NULL
THEN
THEN 'Not
'Not Decimal'
Decimal'
ELSE
ELSE 'Decimal'
'Decimal'
END
END AS
AS Result
Result ;;
data types
DECLARE
DECLARE
DECLARE
DECLARE
DECLARE
DECLARE
@Salary
@Salary decimal(18,2)
decimal(18,2) == 78000.00;
78000.00;
@Annual
@Annual int
int == 50000;
50000;
@XmlData
@XmlData xml;
xml;
SET
SET @Salary
@Salary == @Annual;
@Annual;
SET
SET @Salary
@Salary +=
+= @Annual;
@Annual;
SET
SET @XmlData
@XmlData == '<Customers>
'<Customers>
<Customer
<Customer CustomerID="10"/>
CustomerID="10"/>
</Customers>';
</Customers>';
concurrency
CREATE
CREATE TYPE
TYPE ProductNumber
ProductNumber
FROM
FROM nvarchar(20)
nvarchar(20) NOT
NOT NULL;
NULL;
GO
GO
CREATE
CREATE TABLE
TABLE Production.ProductConversion
Production.ProductConversion
(
( ProductConversionID
ProductConversionID int
int NOT
NOT NULL
NULL IDENTITY(1,1),
IDENTITY(1,1),
FromProduct
FromProduct ProductNumber,
ProductNumber,
ToProduct
ToProduct ProductNumber
ProductNumber
);
);
Notes
binary
varbinary
varbinary(max)
image
hierarchyid
sql_variant
xml
cursor
table
geometry,
geography
Logon information
Virtual machine
10776A-MIA-SQL1
User name
AdventureWorks\Administrator
Password
Pa$$w0rd
Lab Scenario
A new developer has sought your assistance in deciding
which data types to use for three new tables she is
designing. She presents you with a list of organizational
data requirements for each table. You need to decide on
appropriate data types for each item.
You need to export some data from your existing system
but while being exported, some of the columns need to be
converted to alternate data types.
If you have time, there is another issue that your manager
would like you to address. She is concerned about a lack of
consistency in the use of data types across the
organization. At present, she is concerned about email
addresses and phone numbers. You need to review the
existing data types being used in the MarketDev database
for this and create new data types that can be used in
applications, to avoid this inconsistency.
Lab Review
What data type should I use to store the number of
YTD_Sales, DateOfBirth?