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.
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 ratings0% 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.
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.
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
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
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
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.
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
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
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
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;
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;
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)
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
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
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;
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