Posts

Showing posts with the label tsql

Sql Triggers-Implementation Issue

Sql Triggers-Implementation Issue Hi guys i am making 2 tables for implementing triggers-- create table emp(id int primary key identity(1,1), name char(40), salary varchar(50), gender char(40), departmentid int); insert into emp(name,salary,gender,departmentid) values ('jimmi',4800,'Male',4); create table emp_audit ( id int primary key identity(1,1), audit varchar(60) ); alter trigger trigger_em_update on emp for update as begin Declare @Id int Declare @oldname char(40),@newname char(40) Declare @oldsal int,@newsal int Declare @oldgen char(40),@newgen char(40) Declare @olddeptid int,@newdeptid int Declare @auditstring nvarchar(max) --select * from deleted; select * into #temptable from inserted; while(Exists(select id from #temptable)) --boolean condition if there are rows are not Begin set @auditstring ='' --if there are many rows we still select the first one select Top 1 @Id =id,@newname=name,@newgen=gender,@newsal=salary,@newdeptid=departmentid ...

Combining “like” rows

Combining “like” rows I have a table of locations_from, locations_to, and count of flights. I want to combine the rows where departure on one row equals arrival on the other (for example LA TO NY combined with NY TO LA) and SUM the like rows. I think it would be better explained with an example. BEFORE locations_from locations_to # of Flights -------------------------------------------------- San Francisco, CA Los Angeles, CA 29558 Los Angeles, CA San Francisco, CA 32389 New York, NY Los Angeles, CA 30389 Los Angeles, CA New York, NY 35484 Las Vegas, NV Los Angeles, CA 28363 Los Angeles, CA Las Vegas, NV 34455 Honolulu, HI Kahului, HI 46563 Kahului, HI Honolulu, HI 16879 San Francisco, CA New York, NY 44654 New York, NY San Francisco, CA 25882 AFTER From/To From/To # of Flights --------------------------------------------------- San Francisco, CA ...

SQL how to exclude bank Holiday and Christmas DAY on Date diff

SQL how to exclude bank Holiday and Christmas DAY on Date diff Any advice will be highly appreciated How to exclude Bank holidays, Christmas DAY in date datediff in the UK I have a customer table with all the customer booking in a hotel, I would like to calculate the length of stay but I want to exclude bank holidays and other holidays in the UK. Many thanks SELECT [StartDate], DATEDIFF(DAY,[StartDate],[EndDate]) AS Bookingdays ,[EndDate] ,[CustomerId] ,[BookingID] FROM [CustomerBooking] Search for Calendar Table. – Dan Guzman Jun 30 at 10:51 Elaborating Dan Guzman's comment: (1) create Calendar Table. (2) mark the unique days in the Calendar Table. (3) select the dates from the Calendar Table which are between the [StartDate] and [EndDate] and that the column for unique days is not i...