How can I replace not supported TRY_CONVERT function on an old SQL Server 2008 version?

hvvq6cgz  于 2023-03-17  发布在  SQL Server
关注(0)|答案(2)|浏览(170)

I am not so into database and I am finding the following difficulties. I am working on a pretty old version of SQL Server:

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4042.0 (X64)   Mar 26 2015 21:18:04   Copyright (c) Microsoft Corporation  Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)

The problem is that I have this query:

SELECT [Sottocategoria]
      ,[IdSottocategoria]
      ,[IdCategoria]
      ,[Note]
FROM [dbo].[PROT_TITOLARIO]
ORDER BY TRY_CONVERT(hierarchyid,'/'+REPLACE(IdSottocategoria,'.','/')+'/')

that is going into error:

Msg 195, Level 15, State 10, Line 6
'TRY_CONVERT' is not a recognized built-in function name.

I think that I can't increase the compatibility level of the DB because it is too old, infact doing:

ALTER DATABASE GHELLA_CRI SET COMPATIBILITY_LEVEL = 110

I obtain this error message:

Msg 15048, Level 16, State 1, Line 1
Valid values of the database compatibility level are 80, 90, or 100.

So the problem seems to be this TRY_CONVERT function that seems not be supported by SQL Server 2008.

There is a way to replace it with something similar supported by this SQL Server version?

Here a query sample: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=c0650397514372818b10958901c98add

bnlyeluc

bnlyeluc1#

Filling somes blanks here with my own guesses, but basing on what the OP has, I suspect we could do something like this instead:

SELECT *
FROM [dbo].[PROT_TITOLARIO] PT
     CROSS APPLY (VALUES(CASE WHEN PT.IdSottocategoria LIKE '%[^0-9.]%' THEN NULL ELSE PT.IdSottocategoria END)) TC(IdSottocategoria)
     CROSS APPLY (VALUES(CONVERT(hierarchyid,'/'+REPLACE(TC.IdSottocategoria,'.','/')+'/')))V(Hid)
ORDER BY CASE WHEN V.Hid IS NULL THEN 1 ELSE 0 END,
         V.Hid,
         PT.IdSottocategoria;

I use a CASE expression to check that the value of IdSottocategoria only contains numerical values and . 's and then just CONVERT those values. That works for the sample values the OP supplied: db<>fiddle

To reiterate my comments, SQL Server 2008 R2 SP2 is a really old version of SQL Server. SP3 came out some time ago for 2008R2, and SP2 is not patched against the Spectre and Meltdown vulnerabilities. Even if you aren't updating the version of SQL Server for a bit (which should heavily be reconsidered) I cannot more strongly suggest you get that server updated to SP3 GDR . This is especially true if you are in a country that has GDPR (or similar) legislation, as your local authority will see unpatched (and unsupported) software as a huge concern and will not reflect well on the list of preventative measures in the result of a breach.

cwtwac6a

cwtwac6a2#

Could try wrapping it in a try/catch block like this

declare @vDate DATETIME

BEGIN TRY

SELECT @vDate = CONVERT(datetime, '12/31/2010')

END TRY

BEGIN CATCH

SELECT @vDate = null

END CATCH

SELECT @vDate

BEGIN TRY

SELECT @vDate = CONVERT(datetime, 'eggs')

END TRY

BEGIN CATCH

SELECT @vDate = null

END CATCH

SELECT @vDate

相关问题