Apply multiple count conditionally in SQL Server query

I have a task table which contains some DateTime type columns like completed, pending, exception. It joins user table and I want to bring userName, AllTaskCount, AllPendingTaskCount, …

Showing conditional Where Condition in SQL SERVER

I am trying to achieve something like this. SELECT Name, Age From Employee WHERE ID=1 AND CASE(WHEN backguid=’00000000-0000-0000-0000-000000000000′ THEN (State=@state and City = @city) ELSE (…

How to findout a number between two column in SQL Server 2008

Suppose I have table called dbo.tbl_Search Id From To ——————- 1 200 210 2 212 220 3 203 215 4 225 240 Suppose, I search for No.-205 then I would to …

How to select duplicate records for a column having Data/NULL values in other columns?

I have a table in SQL server with 2.5 M records and I want to select the unique records as in image below. Can someone help me write a select query for this? Original table and requested data:

Pass a table variable using table type into a stored procedure?

I have a table employee with around 1000 entries, I have to create a stored procedure which accepts a parameter. The parameter will most likely be of the type which I created ‘AS Table’. Upon passing …

How to search for separated values in cloumns from a merged values column

I have a database where the data I need to work with is stored into two different columns. I also need to import an excel file and the data in this excel file is all together only separated by a dash. …

How to add next and subtract preceding row in SQL Server based on condition

I am trying to calculate the cumulative sum based on TranslationType column in SQL Server. Sample data: Id TransactionType Value ————————- 1 Receipt 10 2 Issue …

Sort by Name first then number in sql query from a delimited string

We have designed to register customer’s station-division records in 1 field. So the table rows looks like this. create table test4 (f2 varchar(100)) insert into test4 (f2) values (‘A08-0100’) insert …

SQL Format(‘MMMM’) does not work in SQL Server 2008

I had this query select format([time], ‘MMMM’) as ‘Month’, count([time]) as ‘Application Usage’, count([time]) as ‘Application Usage’ from UserLogs UL where [time] >= …

Stored Procedure Data Type Conversion Error

I try to execute the following SP it gives me this following error. Msg 8114, Level 16, State 5, Procedure TEST, Line 55 [Batch Start Line 12] Error converting data type nvarchar to …