Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

210


Part II: Building Databases and Working with Data


SELECT TRY_PARSE('Monday, 13 December 2010' AS datetime2 using
'en-US') AS Result

Result:

Result
---------------------
2010-12-13 00:00:00.0000000

The difference between the two functions is that when using the Parse function if the con-
version fails, an error is raised. However, if you use the Try_Parse function, a null value is
returned.

The last conversion introduced in SQL Server 2011 is Try_Convert(data type,
expression style). This function is the counterpart to the existing Convert function
just as the Try_Parse is to Parse. When using the Try_Convert if the conversion fails,
it returns a null value. The following script illustrates how to execute the Try_Convert
function:

SELECT TRY_CONVERT(DATE,'19720526', 112) Results

Result:

Results
-----------
1972-05-26

Server Environment Information .........................................................................


System functions return information about the current environment. This section covers
the two more commonly used system functions.

DB_NAME(): Returns the name of the current database, as shown in the following example:

SELECT CURRENT_TIMESTAMP AS [Date],
DB_NAME() AS [Database];

Result:

Date Database
------------------------- -------
2009-11-15 18:38:50.250 CHA2

SERVERPROPERTY(): Several useful pieces of information about the server may be deter-
mined from this function, including the following:

■ (^) Collation: The collation type
■ (^) Edition: Enterprise, Developer, Standard, and so on
■ (^) EngineEdition: 2 = Standard, 3 = Enterprise, 4 = Express
c08.indd 210c08.indd 210 7/30/2012 4:21:17 PM7/30/2012 4:21:17 PM
http://www.it-ebooks.info

Free download pdf