I have three tables in SQL server; Event table: CREATE TABLE [dbo].[Event] ( [EventName] VARCHAR(25) NOT NULL, [Description] VARCHAR(60) NULL, [Location] VARCHAR(20) NULL, […
Tag: sql-server
How to update “row number” column values 1..N after rows are DELETED?
I have this table: When I make a delete of a box in this table, I need to make an increment of all the following rows. For example: if I delete BoxNo ‘4’ then the rows which are after the last row of BoxNo ‘4’ (5,6,7,..etc) have to make an increment by 1 and be like (4,5,6,…etc.)…
Pivot some row values to a columns
I am trying to pivot some row values to a column, below is what I am trying to achieve. My Current Schema: This is what I am trying to achieve: but it skips rows when name and lan have similar values, in this case it skips the row with the name value vsp. Answer this fixes my problem.
Trigger for automatically updating email field?
I am trying to create a trigger which fires when a new row is inserted into my StudentInformation table without the Email field specified. The trigger should fire and update the email field with …
How do I get this JSON output?
I need to create the following JSON from SQL server 2016. { “recipientList”: [ { “name”: “1”, “recipientType”: “User” }, { “name”: “2”, …
How can I set more than one condition after update statement?
I am trying to change the IsActive status to 0 if any of the other columns get deleted. I wrote this query, but when I run it I get this error: Msg 207, Level 16, State 1, Procedure emp_Delete, …
Join two table by dates and populate the missing data when one table has missing dates
In SQL Server, I have a two tables (tbl1 and tbl2) as shown below. tbl2 contains a list of all dates. In tbl1, some of the dates are missing. tbl1 name nameid count date ———————–…
How to calculate the difference in revenue for 2 specific periods from data in a single table?
I am using SQL Server 2014 and I have the following T-SQL query which is supposed to calculate the difference in total revenue based on 2 specific periods. The data to perform the computation come …
SELECT Brand and Model for cars which have been rented in December 2000?
There is a table called Car with columns CarID, Brand, Model, ProdYear, Color and a table Rent with columns RentID, CarID, RentDate, ReturnDate. How to SELECT Brand and Model for cars which have been rented in December 2000? RentDate and ReturnDate are stored in DATETIME format (YYYY-MM-DD) Answer For a simpl…
Is there an sql script to get the date and time a query was run?
I wondered if anyone can help. I need to get the date and time an sql query was run. I would like the output to be in the following format; YYYY-MM-DDThh:mm:ss Does anyone know what sql to write get …