0% found this document useful (0 votes)
10 views28 pages

Lecture2 Ch08 Datatype

Chapter 8 discusses how to work with various data types in SQL Server, including string, numeric, date/time, and large value data types. It covers ANSI-standard data types, their SQL Server equivalents, and the differences between implicit and explicit data type conversions. Additionally, it provides syntax for data conversion functions such as CAST and CONVERT, along with examples and common date formats.

Uploaded by

minhadcarry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Lecture2 Ch08 Datatype

Chapter 8 discusses how to work with various data types in SQL Server, including string, numeric, date/time, and large value data types. It covers ANSI-standard data types, their SQL Server equivalents, and the differences between implicit and explicit data type conversions. Additionally, it provides syntax for data conversion functions such as CAST and CONVERT, along with examples and common date formats.

Uploaded by

minhadcarry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 8

How to work
with data types

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 1


Objectives
Applied
• Code queries that use the data conversion functions to work with
the data types presented in this chapter.
Knowledge
• Describe the data that can be stored in any of the string, numeric,
date/time, and large value data types.
• Describe the difference between standard character data and
Unicode character data.
• Describe the differences between implicit and explicit data type
conversion.

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 2


SQL Server data type categories
• String
• Numeric
• Temporal (date/time)
• Other

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 3


ANSI-standard data types
and SQL Server equivalents
Synonym for ANSI-standard SQL Server data type used
data type
binary varying varbinary
char varying varchar
character varying
character char
dec decimal
double precision float
float real or float
integer int

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 4


ANSI-standard data types
and SQL Server equivalents (continued)
Synonym for ANSI-standard SQL Server data type used
data type
national char nchar
national character
national char varying nvarchar
national character varying
national text ntext
rowversion timestamp

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 5


The integer data types
Type Bytes
bigint 8
int 4
smallint 2
tinyint 1
bit 1

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 6


The decimal data types
Type Bytes
decimal[(p[,s])] 5-17
numeric[(p[,s])] 5-17
money 8
smallmoney 4

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 7


The real data types
Type Bytes
float[(n)] 4 or 8
real 4

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 8


Terms
• Precision
• Scale
• Exact numeric data types
• Floating-point number
• Significant digits

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 9


String data types for storing standard characters
Type Bytes
char[(n)] n
varchar[(n)]

String data types for storing Unicode characters


Type Bytes
nchar(n) 2×n
nvarchar(n)

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 10


Terms
• Unicode characters
• Fixed-length strings
• Variable-length strings

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 11


Date/time data types prior to SQL Server 2008
Type Bytes
datetime 8
smalldatetime 4
Date/time data types for SQL Server 2008 and later
Type Bytes
date 3
time(n) 3-5
datetime2(n) 6-8
datetimeoffset(n) 8-10

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 12


Common date formats
Format Example
yyyy-mm-dd 2012-04-30
mm/dd/yyyy 4/30/2012
mm-dd-yy 4-30-12
Month dd, yyyy April 30, 2012
Mon dd, yy Apr 30, 12
dd Mon yy 30 Apr 12

Common time formats


Format Example
hh:mi 16:20
hh:mi am/pm 4:20 pm
hh:mi:ss 4:20:36
hh:mi:ss:mmm 4:20:36:12
hh:mi:ss.nnnnnnn 4:20:36.1234567

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 13


How to use date/time literals
• To code a date/time literal, enclose the date/time value in single
quotes.
• If you don’t specify a time in a date/time value, the time defaults
to 12:00 a.m.
• If you don’t specify a date in a date/time value, the date defaults
to January 1, 1900.
• By default, the years 00 to 49 are interpreted as 2000 to 2049 and
the years 50 through 99 are interpreted as 1950 through 1999.
• You can specify a time using either a 12-hour or a 24-hour clock.
• For a 12-hour clock, am is the default.

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 14


The large value data types
for SQL Server 2005 and later
• varchar(max)
• nvarchar(max)
• varbinary(max)
How the large value data types map
to the old large object types
SQL Server 2005 and later Prior to 2005
varchar(max) text
nvarchar(max) ntext
varbinary(max) image

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 15


Order of precedence for common data types
Precedence Category Data type
Highest Date/time datetime
smalldatetime
Numeric float
real
decimal
money
smallmoney
int
smallint
tinyint
bit
String nvarchar
nchar
varchar
Lowest char

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 16


Conversions that can’t be done implicitly
From data type To data type
char, varchar, nchar, nvarchar money, smallmoney
datetime, smalldatetime decimal, numeric, float, real, bigint,
int, smallint, tinyint, money,
smallmoney, bit
money, smallmoney char, varchar, nchar, nvarchar

Expressions that use implicit conversion


InvoiceTotal * .0775
-- InvoiceTotal (money) converted to decimal
PaymentTotal – 100
-- Numeric literal converted to money
PaymentDate = '2012-04-05'
-- Date literal converted to smalldatetime value

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 17


Terms
• Implicit conversion
• Explicit conversion

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 18


The syntax of the CAST function
CAST(expression AS data_type)

A SELECT statement that uses the CAST function


SELECT InvoiceDate, InvoiceTotal,
CAST(InvoiceDate AS varchar) AS varcharDate,
CAST(InvoiceTotal AS int) AS integerTotal,
CAST(InvoiceTotal AS varchar) AS varcharTotal
FROM Invoices;

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 19


How to convert data when performing
integer division
Operation Result
50/100 0
50/CAST(100 AS decimal(3)) .500000

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 20


The syntax of the CONVERT function
CONVERT(data_type, expression [, style])

Convert and format dates


SELECT CONVERT(varchar, InvoiceDate) AS varcharDate,
CONVERT(varchar, InvoiceDate, 1) AS varcharDate_1,
CONVERT(varchar, InvoiceDate, 107) AS varcharDate_107,
CONVERT(varchar, InvoiceTotal) AS varcharTotal,
CONVERT(varchar, InvoiceTotal, 1) AS varcharTotal_1
FROM Invoices;

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 21


Style codes for converting date/time data
to character data
Code Output format
0 or 100 (default) Mon dd yyyy hh:miAM/PM
1 or 101 mm/dd/yy or mm/dd/yyyy
7 or 107 Mon dd, yy or Mon dd, yyyy
8 or 108 hh:mi:ss
10 or 110 mm-dd-yy or mm-dd-yyyy
12 or 112 yymmdd or yyyymmdd
14 or 114 hh:mi:ss:mmm (24-hour clock)

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 22


Style codes for converting real data
to character data
Code Output
0 (default) 6 digits maximum
1 8 digits; must use scientific notation
2 16 digits; must use scientific notation

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 23


Style codes for converting money data
to character data
Code Output
0 (default) 2 digits to the right of the decimal
point; no commas to the left
1 2 digits to the right of the decimal
point; commas to the left
2 4 digits to the right of the decimal
point; no commas to the left

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 24


The syntax of the TRY_CONVERT function
TRY_CONVERT(data_type, expression [, style ])

Convert and format dates


SELECT TRY_CONVERT(varchar, InvoiceDate) AS varcharDate,
TRY_CONVERT(varchar, InvoiceDate, 1) AS varcharDate_1,
TRY_CONVERT(varchar, InvoiceDate, 107)
AS varcharDate_107,
TRY_CONVERT(varchar, InvoiceTotal) AS varcharTotal,
TRY_CONVERT(varchar, InvoiceTotal, 1)
AS varcharTotal_1,
TRY_CONVERT(date, 'Feb 29 2011') AS invalidDate
FROM Invoices;

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 25


Other data conversion functions
• STR(float[,length[,decimal]])
• CHAR(integer)
• ASCII(string)
• NCHAR(integer)
• UNICODE(string)

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 26


Examples that use the data conversion functions
Function Result
STR(1234.5678, 7, 1) 1234.6
CHAR(79) O
ASCII('Orange') 79
NCHAR(332) O
UNICODE(N'Or') 332

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 27


ASCII codes for common control characters
Control character Value
Tab Char(9)
Line feed Char(10)
Carriage return Char(13)

Use the CHAR function to format output


SELECT VendorName + CHAR(13) + CHAR(10)
+ VendorAddress1 + CHAR(13) + CHAR(10)
+ VendorCity + ', ' + VendorState + ' ' +
VendorZipCode
FROM Vendors
WHERE VendorID = 1;
US Postal Service
Attn: Supt. Window Services
Madison, WI 53707

Book Title, C1 © 2012, Mike Murach & Associates, Inc. Slide 28

You might also like