tsql

T-SQL to determine “out of sequence” records

試著忘記壹切 提交于 2021-02-08 15:47:10
问题 I am using Microsoft SQL server and I need to determine records that are "out of sequence" from a table. I'll explain through an example. I have the following table structure: OrderNumber OrderStatus EventDateTime 0001522989 22 2014-04-14 05:49:25.4414243 0001522989 26 2014-04-14 05:51:16.7047485 0001522989 23 2014-04-14 05:51:17.8602798 0001522990 23 2014-04-14 05:51:19.9603575 0001522990 24 2014-04-14 05:52:06.5803494 0001522990 24 2014-04-14 05:52:06.5803494 Now I need to produce a list of

SQL: Convert an integer into a hex string?

走远了吗. 提交于 2021-02-08 14:01:30
问题 How do you convert an integer into a string of hex? I want to convert the int into a format that I can use as a color on my page for example '#ff0000'. So for example: --This converts my int to hex: CONVERT(VARBINARY(8), Color) Color, And I want to do something like this: '#' + CONVERT(NVARCHAR(10), CONVERT(VARBINARY(8), Color)) Color But converting a varbinary string just converts it to an ascii character rather than returning the actual hex string 回答1: There is a built in function to

Why does the Difference function give different results when switching order of strings to compare?

☆樱花仙子☆ 提交于 2021-02-08 12:55:06
问题 In SQL Server, if I do the following: Difference ('Kennady', 'Kary') : I get 2 If i do: Difference ('Kary', 'Kennady') : I get 3. I thought the Difference function looks at the Soundex values under the hood, and gives a 0-4 number of how many characters in place are the same. SELECT SOUNDEX('Kennady') AS [SoundEx Kennady] , SOUNDEX('Kary') AS [SoundEx Kary] , DIFFERENCE ('Kennady', 'Kary') AS [Difference Kennady vs Kary] , DIFFERENCE ('Kary', 'Kennady') AS [Difference Kary vs Kennady]; 回答1:

How to add a dash between running numbers and comma between non-running numbers

血红的双手。 提交于 2021-02-08 12:07:55
问题 I would like to replace a set of running and non running numbers with commas and hyphens where appropriate. Using STUFF & XML PATH I was able to accomplish some of what I want by getting something like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 15, 19, 20, 21, 22, 24 . WITH CTE AS ( SELECT DISTINCT t1.ORDERNo, t1.Part, t2.LineNum FROM [DBName].[DBA].Table1 t1 JOIN Table2 t2 ON t2.Part = t1.Part WHERE t1.ORDERNo = 'AB12345') SELECT c1.ORDERNo, c1.Part, STUFF((SELECT ', ' + CAST(LineNum AS VARCHAR(5))

How to add a dash between running numbers and comma between non-running numbers

▼魔方 西西 提交于 2021-02-08 12:05:15
问题 I would like to replace a set of running and non running numbers with commas and hyphens where appropriate. Using STUFF & XML PATH I was able to accomplish some of what I want by getting something like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 15, 19, 20, 21, 22, 24 . WITH CTE AS ( SELECT DISTINCT t1.ORDERNo, t1.Part, t2.LineNum FROM [DBName].[DBA].Table1 t1 JOIN Table2 t2 ON t2.Part = t1.Part WHERE t1.ORDERNo = 'AB12345') SELECT c1.ORDERNo, c1.Part, STUFF((SELECT ', ' + CAST(LineNum AS VARCHAR(5))

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

徘徊边缘 提交于 2021-02-08 10:32:23
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

≡放荡痞女 提交于 2021-02-08 10:31:54
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

How can I identify which rows in a table have met a certain condition, but the condition is based on data in previous rows? Example provided

痞子三分冷 提交于 2021-02-08 10:26:00
问题 I'm working with a table that contains the following data: ObjectId EventId EventDate 1 342 2017-10-27 1 342 2018-01-06 1 343 2018-04-18 1 401 2018-10-15 1 342 2018-11-12 1 342 2018-11-29 1 401 2018-12-10 1 342 2019-02-21 1 343 2019-04-23 1 401 2019-11-04 1 343 2020-02-15 2 342 2018-06-08 2 343 2018-09-18 2 342 2018-10-02 I need to flag the first record where all 3 events (identified by EventId values 342, 343, and 401) have occurred for an object (identified by ObjectId). Then, the process

MSSQL Deadlock when update withc (updlock)

谁说胖子不能爱 提交于 2021-02-08 06:25:18
问题 I got deadlock while updating. The transaction level is set to Read Committed . How to avoid deadlock in such situation? In other cases WITH (NOLOCK) and WITH (UPDLOCK) helped. I got the following T-SQL query: IF EXISTS (SELECT 1 FROM DEBTORS_CUSTOMERS WITH (NOLOCK) WHERE DebtorId = @DebtorId AND ClientFCCustomerNumber = @CustomerNumber) UPDATE DEBTORS_CUSTOMERS WITH (UPDLOCK) SET StatusId = @StatusId WHERE DebtorId = @DebtorId AND ClientFCCustomerNumber = @CustomerNumber ELSE INSERT INTO

SQL Update table with cumulative value

只愿长相守 提交于 2021-02-08 05:14:39
问题 I have this table: Date |StockCode|DaysMovement|OnHand 29-Jul|SC123 |30 |500 28-Jul|SC123 |15 |NULL 27-Jul|SC123 |0 |NULL 26-Jul|SC123 |4 |NULL 25-Jul|SC123 |-2 |NULL 24-Jul|SC123 |0 |NULL The reason only the top row has an OnHand value is because I can get this from another table that stores the current qty on hand for any stock code. The other records in the table are taken from another table that logs all the movement for any given day. I want to update the above table so that the OnHand