JSON_VALUE() stopped working in SQL Azure v12? - sql

Since yesterday the function JSON_VALUE() is not working anymore in SQL Azure v12.
Who knows why this disappeared and/or when it will come back?
Probably associated: I'm getting this 12.0 instead of 13.0 version on ##version now ?!?
SELECT *##version* as SqlAzureVersion
Result:
Microsoft SQL Azure (RTM) - 12.0.2000.8 Dec 2 2015 00:01:31 Copyright (c) Microsoft Corporation
SELECT **JSON_VALUE**(N'{"a": "b"}', N'$.a')
Result:
'JSON_VALUE' is not a recognized built-in function name.

Azure SQL Database now officially supports JSON functions see https://azure.microsoft.com/en-us/updates/public-preview-json-in-azure-sql-database/

Related

SQL Server 2017 STRING_AGG Order By

I'm using SQL Server 2017 Microsoft SQL Server 2017 (RTM-GDR) (KB4583456) - 14.0.2037.2 (X64) Nov 2 2020 19:19:59 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro 10.0 (Build 19042: ).
I'm trying to sort the results of the STRING_AGG function.
This works fine
SELECT STRING_AGG(Category,', ') AS Result
FROM LegacyReviews_Categories
This gives me the error "Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('."
SELECT STRING_AGG(Category,', ') WITHIN GROUP ( ORDER BY Category ASC) AS Result
FROM LegacyReviews_Categories
I can repro this on SQL Server 2019 build 15.0.4198.2 too.
It just requires the query to be run in the context of a database set to COMPATIBILITY_LEVEL of 100 (2008).
All other (later) compatibility levels work.
The documentation states
STRING_AGG is available in any compatibility level.
And doesn't mention this.
If you are unable to change the compatibility level of the database to something higher then possibly you will need to fall back to the old XML PATH method of concatenating ordered results.

Is IS_ROLEMEMBER available in SQL Server 2012 or 2008R2

Are these functions available in SQL Server 2012 or older?
select IS_ROLEMEMBER ('db_owner');
select IS_SRVROLEMEMBER ('sysadmin');
I can not find any compatibility information in the online documentation here: https://learn.microsoft.com/en-us/sql/t-sql/functions/is-rolemember-transact-sql?view=sql-server-ver15
Is_RoleMember was introduced in SQL Server 2012.
Is_SrvRoleMember was introduced in SQL Server 2005 itself.
You can see when a particular feature was introduced by going through below links. These changes are coming under Database Engine Enhancements => Database Engine Security Enhancements
What is New in SQL 2005
What is New in SQL 2012
To see all the historical information about SQL Server versions, below link is very helpful: https://www.sqlshack.com/history-sql-server-evolution-sql-server-features/

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

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
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.

Cast to datetime is not working on sql server 2016

I am getting Arithmetic overflow error while executing following query on sql server 2016.
select cast(2958463.99999999 as datetime)
The same query works fine on sql server 2014.
Edit 1- select cast(2958463.9999999 as datetime) works on sql server, the only difference is that it used 7 digits after decimal point instead of 8 as originally posted.
Try the following in 2016 (notice the last digit)
select cast(2958463.99999996 as datetime)
You need to have the newest SQL Server Management studio available.
This is both on instances with Server version 2014 (12.0.4439.1) and 2016(13.0.4435.0).
NOTE:
SQL Server Management Studio 2017 only works with 7 digits on instance 2016(13.0.1601.5)
NOTE2:
SQL Server Management Studio 2014 only works with 7 digits on instance
2016(13.0.4435.0)
SQL Server Management Studio 2014 works with all digits on instance 2016(13.0.1601.5)
SQL Server Management Studio 2014 works will all digits on instance 2014(12.0.4439.1)
You can download the newest version here
SQL Server Management Studio 2017
Tested on SQL Server instance 2016(13.0.4435.0)
Tested on SQL Server instance 2014(12.0.4439.1)
Tested on SQL Server Instance 2016(13.0.1601.5)

How to determine SQL server version number for Compact edition 4 up to full sql server 2008?

Lot's of people have asked and been responded to about how to determine the version of the sql server using things like ##VERSION, or SERVERPROPERTY('productversion') but none of these work with sql server compact edition 4.
Is there some universally supported method to determine which sql server edition and version is in use through a sql query or ado.net code that works for compact edition all the way to full sql server?
I want to determine which exact edition / version of SQL server is in use so I know what type of paging query to issue from my code. Sql CE 4 uses a new format for paging queries same as sql server 2011 (denali) and 2005 and 2008 have their own method that is unsupported in CE 4.
I think the answer is that it's impossible but I just want to be sure I didn't overlook something.
I don't really work with SQL Server anymore but here is my attempt at this little problem.
For version 4 of compact edition the following should give you the version and build.
var ver = new System.Data.SqlServerCe.SqlCeConnection().ServerVersion;
And the following should give you the assembly version
var version = typeof(System.Data.SqlServerCe.SqlCeConnection).Assembly.GetName().Version;
Take a look at this blog post. It has a link to download a utility that detects which version of SQL Compact edition you're running. It also has a link to the source code for the utility which may be of interest to you.
You can use PowerShell , in versions of Windows 7 or newer , it comes pre- installed by default. Use the following command lines :
[System.Reflection.Assembly]::LoadFrom("SQLCeAssembly.dll").GetName().Version
Outputs this:
Major Minor Build Revision
----- ----- ----- --------
4 0 0 0
run this
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
See details here