Tag: sql

Trying to count the number of distinct values within my defined categories in SQL Server

I am trying to get the number of distinct Business ID’s that fall into each of the categories that I defined in a sub query. I am wondering if this is possible, and if so, how would I go about doing it. So far this code counts the number of businesses and then returns units […]

don`t understand how trigger works

Trigger should react to deleting row from table Suppliers if there is some supplies in the table Supplies from this supplier, trigger should cancel deleting. This code allows to delete both, suppliers with supplies and suppliers without supply: CREATE TRIGGER SuppliersDeleteCondition ON Suppliers FOR DELETE AS IF EXISTS ( SELECT 1 FROM Supplies Ses JOIN […]

Import multiple delimited text files into a SQL Server database and automatically create tables

I have multiple delimited text files (e.g. .csv files), each containing columns, rows and a header. I want to import all of these input files into SQL Server with as much ease as possible. Specifically, I want to create the output tables into which I will import these files on the fly. Some of these […]

how to substring an empty phrase in sql?

I have a link and I want to substring it into many pieces to get numbers example:- &OR=-12&MR=24560&MM=20&Mx=110&EOW=1&OW=50&OV=12&CV=-1 I want the -12 , 24560 , 20 , … etc. and I did it using this code DECLARE @strprog AS NVARCHAR(MAX)–,@strLM AS NVARCHAR(20),@strLOW AS NVARCHAR(20),@strLOR AS NVARCHAR(20),@strLCR AS NVARCHAR(20), DECLARE @OR AS SMALLINT, @MR AS SMALLINT, […]

Aggregate function with image data type

What is the best way to perform an aggregate function on an image data type? For example, where binImage is image data type, and I want to return either binImage or null: max( case when somechar = ‘A’ and anotherchar = ‘B’ then binImage else null end ) as result … will not work, because […]

Add “- All” as default parameter in my SSRS binary parameter

I have some parameter in a SSRS. I add a “- All” parameter as default value in all of them. For one parameter, a binary one, this cause this error : Syntax error converting the nvarchar value ‘- All’ to a column of data type bit. Here is my Query : SELECT mValue.value AS V […]

Query Subquery Results in SQL

I am relatively new to SQL and I may be over thinking it but given the following table.I want a list of all students that are enrolled in Chemistry but not Mathematics.So given the data I would have Student 1. Student_ID Subject 1 Chemistry 2 Mathematics 2 Chemistry 3 History Here’s what I tried SELECT […]

Joining two tables in SQL Server when one table has huge data and other has few data

If I want to join two tables (not inner join), Left table has huge data (millions of record), and right table has few records. What should I prefer (Left or Right Outer join) and why.

How can I optimize this Update join query in SQL Server 2014?

I am using an update join query to update some records. I am actually joining an indexed table to itself, and updating where a pattern is met. This query worked fine for about a million records, but with 14 million records it just doesn’t scale. The reason I am doing it this way is because […]

Get time between two times on night to morning

I have a table that stores schedules by initial hour to end hour, some structure like | SCHEDULE_NAME | START_HOUR | END_HOUR | | ————- | ————- | ——— | | Morning Shift | 8:00 | 10:00 | | Evening Shift | 13:00 | 17:00 | | Night Shit | 22:00 | 2:00 | So […]

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