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
2条答案
按热度按时间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:
I use a
CASE
expression to check that the value ofIdSottocategoria
only contains numerical values and.
's and then justCONVERT
those values. That works for the sample values the OP supplied: db<>fiddleTo 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.
cwtwac6a2#
Could try wrapping it in a try/catch block like this
declare @vDate DATETIME
BEGIN TRY
END TRY
BEGIN CATCH
END CATCH
SELECT @vDate
BEGIN TRY
END TRY
BEGIN CATCH
END CATCH
SELECT @vDate