Skip to content
Advertisement

Tag: sql-server

Trigger to update a table whenever there is an insert

I am trying to use a trigger (in SQL Server) to update a table whenever there is an insert on the table but I get an error: Conversion failed when converting date and/or time from character string. Trigger used: The table table_scan is to be updated when there is NULL in start_date after an insert happens. Answer Use IS NULL

How to remove duplicates out of the UNION but ignore one column

Consider the following table of data: FirstName LastName Department Steve Colton Accounting Stacy Beckham Finance Mary Messi Finance Steve Colton Finance Michael Gretsky Finance As you can see Steve Colton is in both Accounting and the Finance departments. I want a query that should return Steve just once. I can do the following which works but seems like more code

Creating a table where UserPassword not equal to UserName, but the following code returning a error

In SQL Server: I get this error: Column check constraint for column ‘UserPassword’ references another column, table (Credentials). Answer You are defining column level constraints, In a column level constraint, you can only define constraint for specific column. For your requirement, you should go for table level constraints, from this you can define constraint for multiple columns, like:

How to check for clustered unique key while inserting in SQL table

I am trying to insert rows from another database table to new database table getting the below error if there is no where condition in the query. Violation of UNIQUE KEY constraint ‘NK_LkupxViolations’. Cannot insert duplicate key in object ‘dbo.LkupxViolation’. The duplicate key value is (00000000-0000-0000-0000-000000000000, (Not Specified)). Then I wrote the below query adding where conditions it worked but

Returning multiple aggregated columns from Subquery

I am trying to extend an existing query by aggregating some rows from another table. It works when I only return one column like this: Then I tried to add another column for the aggregated volume: However, this returns the following error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

finding missing dates in a time interval (SQL)

I am a bit of a noob with SQL, so I was searching for some bit of code that might help me find missing date values for a time interval when I stumbled upon this code. Link to the code It works really well for my problem but I am not able to understand how it increments the date. I

Get difference from top ranked item

I’m using the following query to rank items, but I need it to also show the difference between the item and the top ranked item (The top ranked item will show the difference between it and the 2nd ranked item.), like so: How do I create the DiffTop column? Answer You can use window functions for this as well: The

Order By in Union Nested selects

I have two really long selects that are both nested How can I order these (both queries combined into one table results) by NewestMessage which is a type of datetime? Answer I would slightly alter your query to: I am basically replacing your union of tuples to a subquery on a union query, using an ORDER BY with the column

Advertisement