left join of 3 tables according to one table

I have a table of Filters, and a many-to-many table named FilterDoctors , that relates doctors according to which the filter should be made. I want to return the Id of all the filters that either: Has no doctors filter (FilterDoctors.DoctorId IS NULL) OR: Has a doctor that is related to the incident with id […]

Dynamically Count Null Values in SQL Server

I’m a little new at SQL so please bear with me. I am attempting to write some a query that will allow me to loop through an entire table and find the number of times null values appear in each column. This is easy to do the hard way by typing the following: Select SUM(CASE […]

Rollback after update

I have a table with some records: Table: create table test ( column1 varchar(10), column2 varchar(10), column3 varchar(10), ); Insert: insert into test values(‘A’,’B’,’C’); insert into test values(‘E’,’F’,’G’); insert into test values(‘J’,’H’,’I’); Update: By mistake update: Update test set column1 = ‘XYZ’; The above statement updates all rows. The actual update: Update test set column1 […]

From DataTable Insert into Sql Server Database

I’m loading data from a XML file and fill a DataTable. Then I insert that data into SQL Server using a stored procedure. I’m using this code, but it is not working when saving to database: private void button1_Click(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(@”D:\DBXML.xml”); XmlElement root = xmldoc.DocumentElement; XmlNodeList nodes […]

Access command object from SqlDataReader

Having a SqlDataReader object, I am trying to get the timeout of its related command. For example I am using this to create my SqlDataReader: SqlCommand myCommand = new SqlCommand(query, some_connection); _reader = myCommand.ExecuteReader(); In run-time using visual debugger I can access to this using : _reader.Command.CommandTimeout // Visual debugger No how can I do […]

T-SQL – Find distinct values in table only if joined rows satisfy a list of criterias

This is quite a mouthfull for me. One of my challenges is that I don’t know how to formulate the question – which is obvious by the title. I’ll try to illustrate my problem: I have a table, A: ID LocationID 11 185 12 185 13 206 And a table B: ID AID Position Value […]

Count number of equal columns in the same week of several years

I have a table that contains 1 column named Week and 5 columns named n1, n2, n3, n4 and n5. I wanted to count how many of the n1, n2… n5 columns match for each week. So far I created a query that shows all n1…n5 ordered by week select e.Week, e.Year, e.n1, e.n2, e.n3, […]

I Don't Want Null Value When I Count The Name From My Table SQL Server 2008

UserName | Person1 | Person2 | Person3 Kapil | Null | Null | Dks Kapil | Dks | AA | Null Kapil | SKS | AA | Dks Desire Output : User_Name Person Count Kapil Dks 3 Kapil SKS 1 Kapil AA 2 Kapil Null 0 I Don’t Want Count Of Null Value in My […]

sync two local and remote sql server database

I want sync two server in such way when my local server is connected to internet then both database exchange data. I have try server database mirroring but my requirement is little different 1) I want both principal and mirror are present at same time 2) when local server is connected to internet then the […]

Select in Select using parameters

Having a query like the following (shortened and renamed for simplicity) SELECT SOME_COLUMN AS value1, SOME_COMMON_ID as commonID, SOME_TAG as tagID, SOME_TIMESTAMP as endTime, ( SELECT SOME_TIMESTAMP AS beginTime FROM EVENTLIST WHERE EVENTLIST.SOME_TAG = ‘BEGIN’ AND EVENTLIST.SOME_COMMON_ID = commonID /* <– Invalid column name commonID */ ), endTime – beginTime AS duration FROM EVENTLIST JOIN […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.