April 15, 2015 by Muhammad Imran
TRY_CONVERT is one of the new conversion functions shipped with SQL Server. This function converts expression from one data type to another data type. The beauty of this function is that if it fails to convert, it returns NULL value as a results and due to this functionality, it has an edge over CONVERT function. I started using this function wherever conversion required since SQL Server 2012, however, I received an error message this morning when I was trying to use TRY_CONVERT function in SQL Server 2014 as shown below.
Let me explain this error in detail :
Message Number: 195
Severity : 15
Error Message: ‘TRY_Convert’ is not a recognized built-in function name.
Error Generation:
Let me create a sample using TRY_Convert function to demonstrate this error.
USE SampleDB GO SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result; GO SELECT @@VERSION GO
Msg 195, Level 15, State 10, Line 5
‘TRY_CONVERT’ is not a recognized built-in function name.
Microsoft SQL Server 2014 – 12.0.2000.8 (X64)
Ooopps…… I am unable to use TRY_CONVERT even though I am using SQL Server 2014 as shown above.
Resolution:
The resolution is very simple, actually, what you are trying to do is to use TRY_CONVERT function in one of the databases having compatibility less than 110 even though you are using SQL Server 2014. Lets fix this issue step by step.
Step 1: Check compatibility
USE SampleDB; GO SELECT compatibility_level FROM sys.databases WHERE name = 'SampleDB'; GO --OUTPUT
compatibility_level
——————-
100
(1 row(s) affected)
Step 2: Change compatibility
As you can see above the compatibility of the database is below 110, lets change it to enjoy the core functionality of SQL Server 2014.
Note : Test the compatibility change of your database on your test/development database first, before proceeding to production database.
USE master GO ALTER DATABASE SampleDB SET COMPATIBILITY_LEVEL = 120; GO
Step 3: Use TRY_CONVERT
USE SampleDB GO SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result; GO --OUTPUT
Conclusion :
Remember, whenever you use NEW SQL Server functions that are compatible with specific versions / compatibility level, you MUST check the version/ compatibility before implementing those functions.
Posted in Errors, SQL SERVER | Tagged ‘TRY_CONVERT’ is not a recognized built-in function name, Msg 195 Level 15 State 10, raresql, SQL, SQL Server | 2 Comments
Table of Contents
SQL Server Error : 195 Details
SQL Server Error: 195
Severity: 15
Event Logged or not: No
Description:
‘%.*ls’ is not a recognized %S_MSG.
Severity 15 Description:
Indicates syntax errors in the Transact-SQL command.
Reading sql server error log location from SQL Query
Identifying SQL Server Error Log File used by SQL Server Database Engine can be done by reading SQL Server Error Logs. DBA can execute the XP_READERRORLOG extended stored procedure to read the SQL Server Error Log and search for its location used by the instance of SQL Server.
USE master
Go
xp_readerrorlog 0, 1, N'Logging SQL Server messages in file', NULL, NULL, N'asc'
Go
The parameters for XP_READERRRORLOG are:
1. Value of error log file we would like to read. values are 0 = current, 1 = last one before current, 2 = second last before current etc…
2. Log file type:- 1 or NULL = error log, 2 = SQL Agent log
3. Search string 1:- String one you want to search for
4. Search string 2:- String two you want to search for to further refine the results
5. start time for Search
6. end time for search
7. Sort order for search results:- N’asc’ = ascending, N’desc’ = descending
By default, we have 6 Server Error Logs kept but we can increase the number of SQL Server Error Logs from the default value of six.
For other ways to read and find error log location please our artcile https://sqlserver-dba.co.uk/error-log/sql-server-identify-location-of-the-sql-server-error-log-file.html
Solution for Resolving the Error
Alternate Solutions
-
Restarting SQL Server Service(non production instances only)
- To Restart, Start or Stop the SQL Server instance by right click on sql server instance in SSMS or in SQL. You may need to open SSMS as administrator to start, stop the instance.
-
Other ways for restarting SQL server Service
- From SQL Configuration manager from Start menu
- From Services in Windows server
- From Cmd using net start and net stop
2.Checking SQL Performance metrics like CPU, Memory
Check SQL Server CPU, Memory usage, longest running queries, deadlocks etc.. using activity monitor or sp_who2.
To view Activity Monitor in SQL Server 2005 and in SQL Server 2008, a user must have VIEW SERVER STATE permission.
2 Different Ways to Open up Activity Monitor in SQL Server 2008 are mentioned below:
Open up Activity Monitor Using Object Explorer
In Object Explorer, right click the SQL Server 2008 Instance and click on Activity Monitor.
Also can be opened from SQL Server 2008 Management Studio’s toolbar, by clicking Activity Monitor
SSMS Activity Monitor by Method2
It shows the graphical display of Processor Time (%), Number of Waiting Tasks, Database I/O (MB/Sec) and the Number of Batch Requests/second.
For information on SQL Server Activity monitor go to https://sqlserver-dba.co.uk/sql-server-administration-basics/activity-monitor
Or using SQL Query analyzer window to run sp_who2 command which is less resource intensive and gives same information as activity monitor.
2.Checking Windows Performance metrics like CPU, Memory, Disk Space etc.
- Open task manager to check CPU, Memory usage etc.
- Open file explorer to check Disk space on each drive.
SQL Server Error Code and solution summary
SQL Server Error: 195
Severity: 15
Event Logged or not: No
Description:
‘%.*ls’ is not a recognized %S_MSG.
On Transact SQL language the Msg 195 Level 15 — Is not a recognized built-in function name means that the function name is misspelled or does not exist.
Msg 195 Level 15 Example:
Invalid select:
SELECT YEAR123('2014-03-01') as 'Col 1',
YEAR(SYSDATETIME()) as 'Col 2';
Message |
---|
Msg 195, Level 15, State 10, Line 1 |
‘YEAR123’ is not a recognized built-in function name. |
Correct select:
SELECT YEAR('2014-03-01') as 'Col 1',
YEAR(SYSDATETIME()) as 'Col 2';
Col 1 | Col 2 |
---|---|
2014 | 2014 |
Other error messages:
- There are more columns in the INSERT statement than values specified in the VALUES clause
- Must declare the scalar variable
- Is not a recognized datepart option
- The definition for column must include a data type
- Invalid column name
- Operand type clash: int is incompatible with date
- Invalid object name
|
|
While running the following code on a database on my SQL Server 2012 instance I got the following error message
USE BB go SELECT TRY_CONVERT(xml, 'MyClub')
The reason for this error is that this function TRY_CONVERT is 110 compatible. Which means it will only run on a database with 110 compatibility. Upon checking the compatibility level, I found the database to be on 90 or SQL Server 2005 compatible.
Resolution:
I had to change the compatibility level to 110 and then ran the code with success.
Tagged: Analytical functions in SQL Server 2012, Error message, Functions, Microsoft SQL Server, New features in SQL Server 2012, Troubleshooting, TRY_CONVERT