sql-server-2016

TRANSLATE function in SQL SERVER

我的未来我决定 提交于 2019-12-11 16:46:43
问题 I read that there is a function equivalent to the standard function TRANSLATE under DB2 under SQL Server 2017. But how to do under earlier versions? For definition of function : here 回答1: EDITED: I'm feeling dumb - MatBailie correctly pointed out that my original solution was incorrect. I actually always thought that TRANSLATE('abc', 'abc', 'bcd') was supposed to return ddd but, after testing SQL Server 2017's TRANSLATE I see that 'bcd' would be the correct answer. You can see my original

Using multipart column identifiers

被刻印的时光 ゝ 提交于 2019-12-11 15:08:59
问题 I'm sure this has to be documented SOMEWHERE but for the life of me I just can't seem to find any actual documentation explaining the behavior. Taking the 4 ways to reference tables (I don't believe there are more but feel free to correct me): Current Database Remote Database Linked Server Synonym Their behavior when using multi-part column identifiers seem to differ and I'm trying to understand the reasoning behind it. I've tested the various types of SELECT statements: Current Database

tSQLt Testing SQL Server security permissions

谁都会走 提交于 2019-12-11 14:57:58
问题 Is there a way to test security permissions in tSQLt? Currently I am trying to test an accounts security and the results return from tSQLt are not what I am expecting. I am expecting on error that the account does not have permissions, but it appears that the "test" is using my account permissions and succeeding. EXEC tSQLt.NewTestClass @ClassName = N'TestSecurity' -- nvarchar(max) go CREATE PROCEDURE TestSecurity.[test execute as security] AS BEGIN EXECUTE AS USER = 'account3' SELECT * FROM

How to mask data values from a group of users?

谁说我不能喝 提交于 2019-12-11 14:43:27
问题 I'm a (junior) DWH/BI Dev and want to remove permission for seeing unmasked data for whole our group (but still be able to revoke that anytime). I've created a table with MASKED WITH (FUNCTION = 'partial(1,"xxxx",0)') -like defined columns and now want to set up the permission. I've tried: DENY UNMASK TO PCCZ\CZ_GBUS_ITD_BI EXECUTE AS USER = 'PCCZ\CZ_GBUS_ITD_BI' SELECT * FROM dim.MaskingTest REVOKE UNMASK TO PCCZ\CZ_GBUS_ITD_BI; 回答1: You are not able to DENY UNMASK on sysadmin and db_owner .

SQL Server - Using JSON to return Column Names

我们两清 提交于 2019-12-11 09:47:44
问题 I have the following test query I'm needing to make dynamic. Basically a stored procedure will be passed @json and it needs to return the column names that are passed in that variable. Is this possible and how could I do it? declare @json varchar(max) set @json = '["FirstName", "LastName","DOB"]'; select * from OPENJSON( @json ) select FirstName, LastName, DOB from Client I do have this that works, but not sure on whether it's a good option and whether there's a better way declare @json

Retrieving output parameter value along with other return values from select statement having more than one return value

血红的双手。 提交于 2019-12-11 08:32:09
问题 Hi i have an output parameter to a stored procedure and its value has to be returned from the two internal select statements. I have @nTotalRecords as my output parameter that value will come from the below select statement how should i retrieve the output parameter from the alias table TBL in this case i tried like this create procedure [usp_GetMessagesbyReferenceID1] ( @nRowsPerPage int, @nPage int, @nTotalRecords int output ) as select TBL.createdate, TBL.templateid, @nTotalRecords=TBL

TSQL: Find a date with varying characters in a string

不问归期 提交于 2019-12-11 07:49:09
问题 I need to find a continuous date in a string from column name Filename. The string has other numbers in it with dashes(or another character, like an underscore), but I only need the continuous number The Date needs to be extracted from the filename. (I know the data is just wow, multiple vendors, multiple file naming formats is the cause.) This question is similar to this question, but it's looking for something different with a different requirement: TSQL: Find a continuous number in a

Find JSON Schema in SQL Server

℡╲_俬逩灬. 提交于 2019-12-11 06:56:55
问题 I have JSON in a field, but I need to check it's schema before I process it. I need to know if anything has been added or removed from the schema. Is there a way to extract the JSON schema from a JSON string so I can compare it to a known schema? An online example is http://jsonschema.net/, but I want to do the same thing in TSQL 回答1: SQL Server don't support any json schema binding. If your JSON is simple or flat, you can use SELECT [key] FROM OPENJSON(@json) to find all keys on the first

SQL split string and get NULL values instead of empty string

社会主义新天地 提交于 2019-12-11 06:13:00
问题 How to split string and get NULL values instead of empty string. I am particularly interested in two methods STRING_SPLIT and XML . I wish this query: SELECT * FROM STRING_SPLIT('a,b,,d', ','); would return in third row NULL instead of empty string. Is there any easy way to achieve it i.e. with special characters? I mean something like: SELECT * FROM STRING_SPLIT('a,b,[null],d', ',') -- this, of course, wouldn't go I wish I could extract NULLs from string using XML method: CREATE FUNCTION dbo

SQL Server - aggregate if only one distinct value + nulls without ansi warnings

孤街浪徒 提交于 2019-12-11 06:06:24
问题 Suppose I have a data like this first_name last_name city John Bon Jovi null John Lennon null John Deer null And I want to create aggregating query which will return json which looks like this { "first_name": "John", "city": null } Essentially, the query should check if there's only one distinct value within each column and if it is, put this value to json. All non-null columns are relatively easy to get with a query like this: select case when count(distinct first_name) = 1 then max(first