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 …

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 …

SQL subquery with latest record

I’ve read just about every question on here that I can find that is referencing getting the latest record from a subquery, but I just can’t work out how to make it work in my situation. I’m creating …

sum columns of a table, grouping by table partition

I have a table which holds daily metrics : |DateID | Metric1 | Metric2| 20190501 24 34 20190502 25 56 ….. …. …. The table has more than 150 million rows. …

Selecting Max Value, However Prioritising Certain Values

I have three tables that are joined. TableA has unique values for Column1 (ID) which joins on TableC on Column1 which has non-unique values. I’m currently joining these based off the max value for …