tsql

Not In operator eliminates NULL values rows in a table

假如想象 提交于 2020-08-08 05:27:06
问题 I would like to retrieve all rows with values except 1,2,3,4,5 in my COLUMNA in TABLEA . SELECT * FROM TABLEA WHERE COLUMNA NOT IN (1,2,3,4,5) But this eliminates the rows with NULL values in COLUMNA too. I don't want to eliminate NULL values rows and would like to include those rows in the resultset. Alternatively, I can try below query for the same SELECT * FROM TABLEA WHERE COLUMNA NOT IN (1,2,3,4,5) OR COLUMNA IS NULL. But I would like to know, why is it necessary to add this OR condition

Need to Pivot String values in SQL server

老子叫甜甜 提交于 2020-08-07 05:26:37
问题 I have table described as: Occupation String | Name String With values: Developer | A Developer | B Designer | X Coder | Y Coder | Z I need values in pivot format as: Designer | Developer | Coder ---------+-----------+-------- X | A | Y Null | B | Z Can anyone help on this ? Thanks in advance 回答1: The basic PIVOT with ROW_NUMBER() will do things for you: SELECT [Developer], [Designer], [Coder] FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY (SELECT NULL)) RN FROM #temp )

what's the difference between WHERE and HAVING

心已入冬 提交于 2020-08-01 05:56:15
问题 It looks like both WHERE and HAVING help filter rows. I wonder if, instead of having to HAVING, I can use WHERE ... AND . I'm a little confused. Thanks for helping 回答1: Explanation and query samples in this fine article: Where Vs Having / Difference between having and Where clause 回答2: You use "WHERE" clauses to select rows for inclusion in the dataset before grouping operations have happend (GROUP BY). You use HAVING clauses to filter rows after grouping. So like if you're aggregating a sum

what's the difference between WHERE and HAVING

坚强是说给别人听的谎言 提交于 2020-08-01 05:56:04
问题 It looks like both WHERE and HAVING help filter rows. I wonder if, instead of having to HAVING, I can use WHERE ... AND . I'm a little confused. Thanks for helping 回答1: Explanation and query samples in this fine article: Where Vs Having / Difference between having and Where clause 回答2: You use "WHERE" clauses to select rows for inclusion in the dataset before grouping operations have happend (GROUP BY). You use HAVING clauses to filter rows after grouping. So like if you're aggregating a sum

SELECT with a Replace()

浪子不回头ぞ 提交于 2020-07-31 06:57:11
问题 I have a table of names and addresses, which includes a postcode column. I want to strip the spaces from the postcodes and select any that match a particular pattern. I'm trying this (simplified a bit) in T-SQL on SQL Server 2005: SELECT Replace(Postcode, ' ', '') AS P FROM Contacts WHERE P LIKE 'NW101%' But I get the following error; Msg 207, Level 16, State 1, Line 3 Invalid column name 'P'. If I remove the WHERE clause I get a list of postcodes without spaces, which is what I want to

Show every week of the Year even if there is no data

大城市里の小女人 提交于 2020-07-31 05:05:12
问题 I have query that pulls data by week and groups it together. But i does not display weeks that doesn't have any data. I want show all weeks even if they don't have data as null maybe Here is the query if someone can help me with this it will awesome SELECT DATEADD (week, datediff(week, 0, StartDate), -1) as 'WeekOf' ,DATEADD (week, datediff(week, 0, StartDate), +5) as 'to' ,DATEPART(wk, StartDate) as 'WeekNumber' FROM [DESOutage].[dbo].[OPSInterruption] Where StartDate > '2020-01-01' and

Replacing multiple commas with single comma in MS SQL

一曲冷凌霜 提交于 2020-07-30 07:28:18
问题 How do I replace consecutive commas in a column with single comma in MS SQL? For example, I have data like a,,,,b,,,c,,,,,, d,e,,,f,,,,,,g,, I want this to be processed to following format: a,b,c, d,e,f,g, The suggested duplicate, Use SQL to Replace Multiple Commas in a String with a Single Comma, is for Oracle. This is a question about SQL Server. 回答1: You could use simple REPLACE : SELECT c, REPLACE(REPLACE(REPLACE(c, ',', '~,'), ',~', ''), '~,', ',') FROM tab; DBFiddle Demo Output: ┌──────

Incorrect syntax near '('. Expecting ID

ぃ、小莉子 提交于 2020-07-29 12:06:08
问题 I've looked through several other questions of this type and have not found one that will help me resolve this issue. What I'm trying to do is this: I want to create a table for all employees for the purpose of assigning an EmployeeID that will be used across several other tables. That table and the others work fine. My issue arises when I try to create a new table based off the EmployeeType so I can bring up information based solely on employees of a specific type. On the SELECT statement

SQL Server XML Processing: Join different Nodes based on ID

点点圈 提交于 2020-07-22 05:14:31
问题 I am trying to query XML with SQL. Suppose I have the following XML. <xml> <dataSetData> <text>ABC</text> </dataSetData> <generalData> <id>123</id> <text>text data</text> </generalData> <generalData> <id>456</id> <text>text data 2</text> </generalData> <specialData> <id>123</id> <text>special data text</text> </specialData> <specialData> <id>456</id> <text>special data text 2</text> </specialData> </xml> I want to write a SELECT query that returns 2 rows as follows: DataSetData |

SQL Server - BEFORE INSERT Trigger

浪子不回头ぞ 提交于 2020-07-21 03:52:31
问题 I have two tables as follows: Table 1 'MySkills' [ primary key (EmpId, SkillId)] +----------+------------+---------+----------+-----------+------------+-----------------+----------------+ | EmpId | CategoryId | SkillId | ExpYears | ExpMonths | Experience | RatingSubmitted | RatingApproved | +----------+------------+---------+----------+-----------+------------+-----------------+----------------+ | CSSL9610 | arcgis | arcgis1 | 0.00 | 0.00 | 0.00 | 1.00 | NULL | | CSSL9610 | arcgis | arcgis2 |