3. Datetime DATE: As you can imagine, the DATE data type only stores a date in the format of YYYY-MM-DD. It has a range of 0001-01-01 through 9999-12-32, which should be adequate for most business and scientific applications. The accuracy is 1 day, and it only takes 3 bytes to store the date. TIME: TIME is stored in the format: hh:mm:ss.nnnnnnn, with a range of 00:00:00.0000000 through 23:59:59:9999999 and is accurate to 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 3 to 5 bytes.
4. DECLARE @d DATE SET @d = GETDATE() SELECT @d -- outputs '2008-04-06 ' SET @d = '1234-03-22 11:25:09' SELECT @d -- outputs '1234-03-22 '
5. CREATE TABLE #times( T TIME, T1 TIME(1), T5 TIME(5), T7 TIME(7) ) INSERT INTO #times VALUES ( '01:02:03.1234567', '01:02:03.1234567', '01:02:03.1234567', '01:02:03.1234567') SELECT * FROM #times T T1 T5 T7 ---------------- ---------------- ---------------- ---------------- 01:02:03.1234567 01:02:03.1000000 01:02:03.1234600 01:02:03.1234567
6. DATETIME2: DATETIME2 is very similar to the older DATETIME data type, but has a greater range and precision. The format is YYYY-MM-DD hh:mm:ss:nnnnnnnm with a range of 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999, and an accuracy of 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 6 to 8 bytes. DATETIMEOFFSET: DATETIMEOFFSET is similar to DATETIME2, but includes additional information to track the time zone. The format is YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm with a range of 0001-01-01 00:00:00.0000000 through 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 (in UTC), and an accuracy of 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 8 to 10 bytes.
9. Spatial GEOMETRY: The GEOMETRY data type is used to store planar (flat-earth) data. It is generally used to store XY coordinates that represent points, lines, and polygons in a two-dimensional space. For example storing XY coordinates in the GEOMETRY data type can be used to map the exterior of a building. GEOGRAPHY: The GEOGRAPHY data type is used to store ellipsoidal (round-earth) data. It is used to store latitude and longitude coordinates that represent points, lines, and polygons on the earth’s surface. For example, GPS data that represents the lay of the land is one example of data that can be stored in theGEOGRAPHY data type.
12. HIERARCHYID Organizational structures A set of tasks that make up a larger projects (like a GANTT chart) File systems (folders and their sub-folders) A classification of language terms A bill of materials to assemble or build a product A graphical representation of links between web pages
13. CREATE TABLE #Categories ( CategoryID INT IDENTITY(1,1), CategoryNode HIERARCHYID NOT NULL, CategName NVARCHAR(40) NOT NULL )
14. DECLARE @root HIERARCHYID = hierarchyid::GetRoot() INSERT INTO #Categories (CategoryNode, CategName) VALUES (@root, 'All #Categories') -- insert the 'Electronics' category DECLARE @electronics HIERARCHYID SELECT @electronics = @root.GetDescendant(NULL, NULL) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@electronics, 'Electronics') -- insert the 'Music' category after 'Electronics' DECLARE @music HIERARCHYID SELECT @music = @root.GetDescendant(NULL, @electronics) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@music, 'Music')
15. -- insert the 'Apparel' category between 'Electronics' and 'Music' SELECT @music = @root.GetDescendant(@music, @electronics) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@music, 'Apparel') -- insert some children under 'Electronics' DECLARE @video HIERARCHYID -- We could do a simple @category.GetDescendant() but, let's -- show something that is more likely to happen SELECT @video = CategoryNode.GetDescendant(NULL, NULL) FROM #Categories WHERE CategName ='Electronics' INSERT INTO #Categories (CategoryNode, CategName) VALUES (@video, 'Video Equipment') -- insert some children under 'Video Equipment' DECLARE @tvs HIERARCHYID SELECT @tvs = @video.GetDescendant(NULL, NULL) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@tvs, 'Televisions') DECLARE @players HIERARCHYID SELECT @players = @video.GetDescendant(NULL, @tvs) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@players, 'DVD - BluRay')
16. SELECT CategoryID, CategName, CategoryNode, CategoryNode.ToString() AS Path FROM #Categories
17. FILESTREAM Transact-SQL can be used to SELECT, INSERT, UPDATE, DELETE FILESTREAM data. By default, FILESTREAM data is backed up and restored as part of the database file. If you want, there is an option available so you can backup a database without the FILESTREAM data. The size of the stored data is only limited by the available space of the file system. Standard VARBINARY(MAX) data is limited to 2 GB.
18.
19. USE Production; GO CREATE TABLE DocumentStore ( DocumentID INT IDENTITY PRIMARY KEY, Document VARBINARY (MAX) FILESTREAM NULL, DocGUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT NEWID ()) FILESTREAM_ON FileStreamGroup1; GO