sql-server-2016

SQL WHERE depending on day of week

荒凉一梦 提交于 2019-12-22 11:21:29
问题 I have a requirement to check records up to differing dates, depending on which day of the week it is currently. On a Friday I need for it to look at the entire next week, until Sunday after next. On any other day it should check the current week, up until the coming Sunday. I have the below currently but it's not working due to syntax error. Is it possible to do a CASE WHEN inside a WHERE clause? WHERE T0.[Status] IN ('R','P') AND CASE WHEN DATEPART(weekday,GETDATE()) = '5' THEN T0.[DueDate]

NHibernate HQL Generator to support SQL Server 2016 temporal tables

半腔热情 提交于 2019-12-22 01:13:46
问题 I am trying to implement basic support for SQL Server 2016 temporal tables in NHibernate 4.x. The idea is to alter SQL statement from SELECT * FROM Table t0 to SELECT * FROM Table FOR SYSTEM_TIME AS OF '2018-01-16 00:00:00' t0 You can find more info about temporal tables in SQL Server 2016 here Unfortunately, I've not found any way to insert FOR FOR SYSTEM_TIME AS OF '...' statement between table name and its alias. I'm not sure if custom dialects supports this. The only working solution I

SQL Server JSON_Modify, How to Update all?

这一生的挚爱 提交于 2019-12-21 13:41:12
问题 i am trying update all columns with a value with Json_Modify: DECLARE @JSON NVARCHAR(MAX) SET @JSON = N'{ "A":1, "TMP": [ {"A":"VALUE1", "B": "VALUE2", "C": 1}, {"A":"VALUE3", "B": "VALUE4", "C": 2}, {"A":"VALUE5", "B": "VALUE6", "C": 3}]} ' SET @JSON = JSON_MODIFY(@JSON, '$.TMP.A', 'JEJE') SELECT * FROM OPENJSON(@JSON, '$.TMP') WITH ( A NCHAR(10), B NCHAR(10), C INT ) I need update all columns "A" with "JEJE" for example, it is not working. 回答1: Here are two options. Disclaimer: I am not a

SQL Server 2016, Invalid object name 'STRING_SPLIT'

半城伤御伤魂 提交于 2019-12-21 03:17:40
问题 In SQL Server 2016 I receive this error with STRING_SPLIT function SELECT * FROM STRING_SPLIT('a,b,c',',') Error: Invalid object name 'STRING_SPLIT'. 回答1: Make sure that the database compatibility level is 130 you can use the following query to change it: ALTER DATABASE [DatabaseName] SET COMPATIBILITY_LEVEL = 130 回答2: I was using Split_string , which felt grammatically correct in English, but my old eyes didn't see that it should be STRING_SPLIT ... so, if you're a bit mentally challenged

MSSQL 2016 Server: All databases marked Recovery Pending state

核能气质少年 提交于 2019-12-20 07:26:06
问题 Tonight I had run into this issue where all my databases on our production server were in a Recovery Pending state after doing a Windows (server 2016) Update, an update to my Redgate SQL Backup software and restarting the server. Looking at the logs I found that all of the databases were not accessible with an "Access denied" error. So I figured it was something to do with the user under which the MSSQLSERVER service was running. So I explicitly set permissions to the data and log folders for

SQL Server 2016 How to use a simple Regular Expression in T-SQL?

我只是一个虾纸丫 提交于 2019-12-20 06:39:50
问题 I have a column with the name of a person in the following format: "LAST NAME, FIRST NAME" Only Upper Cases Allowed Space after comma optional I would like to use a regular expression like: [A-Z]+,[ ]?[A-Z]+ but I do not know how to do this in T-SQL. In Oracle, I would use REGEXP_LIKE, is there something similar for SQL Server 2016? I need something like the following: UPDATE table SET is_correct_format = 'YES' WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+'); 回答1: First, case sensitivity

Deploying SSIS Package to SQL Server 2016

佐手、 提交于 2019-12-20 03:06:36
问题 I have an SSIS project in VS: Microsoft Visual Studio Professional 2015 Version 14.0.25431.01 Update 3 Microsoft .NET Framework Version 4.6.01590 SQL Server Data Tools 14.0.61116.0 Microsoft SQL Server Data Tools SQL Server Integration Services Microsoft SQL Server Integration Services Designer Version 13.0.1601.5 Project > Properties > Configuration Properties > Deployment Target Version > TargetServerVersion = SQL Server 2016 I deployed and validated successfully on two SQL server instances

SQL Server 2016 - Is it possible to concatenate two nvarchar always encrypted columns?

泪湿孤枕 提交于 2019-12-20 01:41:39
问题 I have created a table using: create table dbo.employee(firstname nvarchar(100) null,lastname nvarchar(100) null) Inserted some sample data using: insert into dbo.employee values('Sachin','Tendulkar') insert into dbo.employee values('Rohit','Sharma') insert into dbo.employee values('Virendra','Sehwag') insert into dbo.employee values('Irfan','Pathan') Then I used always encrypted wizard to encrypt both columns of this table using SSMS v17. And now I am trying to concatenate firstname with

DENSE_RANK() without duplication

痞子三分冷 提交于 2019-12-19 08:03:13
问题 Here's what my data looks like: | col1 | col2 | denserank | whatiwant | |------|------|-----------|-----------| | 1 | 1 | 1 | 1 | | 2 | 1 | 1 | 1 | | 3 | 2 | 2 | 2 | | 4 | 2 | 2 | 2 | | 5 | 1 | 1 | 3 | | 6 | 2 | 2 | 4 | | 7 | 2 | 2 | 4 | | 8 | 3 | 3 | 5 | Here's the query I have so far: SELECT col1, col2, DENSE_RANK() OVER (ORDER BY COL2) AS [denserank] FROM [table1] ORDER BY [col1] asc What I'd like to achieve is for my denserank column to increment every time there is a change in the value

Avoid schema mismatch in System-Versioned tables

末鹿安然 提交于 2019-12-19 05:46:56
问题 Looking for a workaround for: Error: SQL71609: System-versioned current and history tables do not have matching schemes. Mismatched column: 'XXXX'. When trying to use SQL 2016 System-Versioned (Temporal) tables in SSDT for Visual Studio 2015. I've defined a basic table: CREATE TABLE [dbo].[Example] ( [ExampleId] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, [ExampleColumn] VARCHAR(50) NOT NULL, [SysStartTime] datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL, [SysEndTime] datetime2 GENERATED