tsql

SQL Server: determining the Years, Months, Weeks and Days between two dates

泪湿孤枕 提交于 2021-01-04 07:05:46
问题 After reading on this topic and being advised to use DateDiff . I wrote a function that doesn't provide the answer I want. The client wants to now how long it took to complete a checklist. I have a CreationDate and CompletionDate . I need to know how many years, months, weeks and days it took. If it is 2 days then '2 days' without the years. The function deducts the number of years and then attempt to check the number of months, then the number of weeks and then the number of days. Only

SQL Server: determining the Years, Months, Weeks and Days between two dates

偶尔善良 提交于 2021-01-04 07:02:49
问题 After reading on this topic and being advised to use DateDiff . I wrote a function that doesn't provide the answer I want. The client wants to now how long it took to complete a checklist. I have a CreationDate and CompletionDate . I need to know how many years, months, weeks and days it took. If it is 2 days then '2 days' without the years. The function deducts the number of years and then attempt to check the number of months, then the number of weeks and then the number of days. Only

Using OFFSET-FETCH, how to default number of rows to “all rows”?

一个人想着一个人 提交于 2021-01-02 08:16:33
问题 Envision a stored procedure which takes @skip (offset) and @take (maximum number of rows to return. If @take is null , then I want to return "all rows after applying the offset". I can accomplish this by counting the number of rows in the table/view if @take is null , but then I have to perform two queries which is, of course, not optimal. I was hoping there was an option similar to FETCH [NEXT] ALL ROWS . DECLARE @skip BIGINT = 0; DECLARE @take BIGINT = NULL; IF (@take IS NULL) SET @take =

MS SQL Server - Export Results To File without “NULL”

痞子三分冷 提交于 2021-01-02 05:43:26
问题 I am using SQL Server Management Studio to export the results of my query to text. I would like to export the results without the character NULL being printed between delimiters. For example, instead of: ,NULL, I would like to export: ,, Thanks for your help 回答1: Just simply use ISNULL(someColumn, '') in our query. This will replace all NULL values with an empty string 回答2: It depends on how you export the data. If you use the Import and Export Wizard, the NULL strings do not show up in the

Deadlock graph from Extended Events not showing

北战南征 提交于 2021-01-01 13:40:55
问题 I have this query which is from "Sql Server 2017 Query Performance Tuning"(a book). Code for the book can be found here: https://github.com/Apress/sql-server-2017-query-perf-tuning DECLARE @path NVARCHAR(260) --to retrieve the local path of system_health files SELECT @path = dosdlc.path FROM sys.dm_os_server_diagnostics_log_configurations AS dosdlc; SELECT @path = @path + N'system_health_*'; WITH fxd AS (SELECT CAST(fx.event_data AS XML) AS Event_Data FROM sys.fn_xe_file_target_read_file(

Deadlock graph from Extended Events not showing

喜欢而已 提交于 2021-01-01 13:35:14
问题 I have this query which is from "Sql Server 2017 Query Performance Tuning"(a book). Code for the book can be found here: https://github.com/Apress/sql-server-2017-query-perf-tuning DECLARE @path NVARCHAR(260) --to retrieve the local path of system_health files SELECT @path = dosdlc.path FROM sys.dm_os_server_diagnostics_log_configurations AS dosdlc; SELECT @path = @path + N'system_health_*'; WITH fxd AS (SELECT CAST(fx.event_data AS XML) AS Event_Data FROM sys.fn_xe_file_target_read_file(

losing null values filtering sql query results using where

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-31 05:42:13
问题 I have a complex query which joins more than 7 tables. After the joins, I would like to filter the result of my query . Here is something that I observed. When I do a where clause where X.Name != 'xxx' and XY.Product != 1 I get the filtered results , but all the null values for the X.Name and XY.Product also disappear from my result. I would like to retain the null values. I also tried : and X.Name != 'xxx' and XY.Product != 1 I removed the where clause totally and put in an and , but I dont

Comparison Query to Compare Two SQL Server Tables [duplicate]

若如初见. 提交于 2020-12-28 22:51:49
问题 This question already has answers here : sql query to return differences between two tables (12 answers) Closed 3 years ago . I would like to know how to compare two different database table records . What I mean is I will compare two database tables which may have different column names but same data. But one of them may have more records than the other one so I want to see what the difference is between those two tables. To do that how to write the sql query ? FYI : these two databases are

Comparison Query to Compare Two SQL Server Tables [duplicate]

假如想象 提交于 2020-12-28 22:43:40
问题 This question already has answers here : sql query to return differences between two tables (12 answers) Closed 3 years ago . I would like to know how to compare two different database table records . What I mean is I will compare two database tables which may have different column names but same data. But one of them may have more records than the other one so I want to see what the difference is between those two tables. To do that how to write the sql query ? FYI : these two databases are

Insert bulk data into two related tables with foreign keys from another table

喜你入骨 提交于 2020-12-27 06:20:18
问题 I have imported some data to a temp SQL table from an Excel file. Then I have tried to insert all rows to two related tables. Simply like this: There are Events and Actors tables with many to many relationship in my database. Actors are already added. I want to add all events to Events table and then add relation(ActorId) for each event to EventActors tables. (dbo.TempTable has Title, ActorId columns) insert into dbo.Event (Title) Select Title From dbo.TempTable insert into dbo.EventActor