The binary representation of integer 170 is 1 0 1 0 1 0 1 0 Reversing the bits gives 0 1 0 1 0 1 0 1 That converted to decimal is integer 85. Why does SELECT ~170 return -171? I am struggling to understand what I am missing here. ...
more »

2017-10-14 22:10 (1) Answers

Select as JSON joined tables

I would like to select data from 2 tables into one json string. First table should list all linked tables from second table like First table: SELECT [orderNumber], [tcpState] FROM [Tracking] Second table: SELECT [startdate], [enddate], [tcpStat...
more »

2017-10-14 21:10 (1) Answers

SQL Server : parameterized stored procedure

I have to do this in SQL Server. Assume that I have 2 tables. Based on parameters Name and Surname, I have to take PhysicianID from Table1. After that I have to create new record using insert into stored procedure. Something like this CREATE PROC...
more »

2017-10-14 21:10 (2) Answers

SQL Server database backup error

I am trying to solve a backup problem for SQL Server 2012 for some time now. The problem is I not able to make backup TO DISK, I think it is directory error Query used: BACKUP DATABASE [ProjectDatabase] TO DISK = 'C:\Users\AizazHussain\Dropbox\T...
more »

2017-10-14 02:10 (1) Answers

SQL Performance: Filter first or Join first

I am having three tables viz., Employees, Department and Grievance. The Employees table has over a million records. I need to find the details of the employee, his/her department and grievances raised by him/her. I could think of the below two queri...
more »

2017-10-13 12:10 (4) Answers

Varchar in sql creating extra whitespaces

Well, I am building a small hobby program in C#, and I am trying to write information from my program to Sql The thing is I have some varchar fields in Sql with a max length of 50 when I send information to fill those columns they will be added with...
more »

2017-10-13 03:10 (2) Answers

Two Percent Signs in EXEC Statement

In SQL Server 2014, the master database contains a system stored procedure called sp_rename. I was interested to know how it works, so I've been taking a look at it. On line 190, there is an EXEC statement that contains two percent signs: EXEC %%Sc...
more »

2017-10-12 15:10 (0) Answers

How to effective version store procedures?

i am part of database development team wotking for big eshop. We are using MS SQL 2016 and ASP.NET. SQL Server is used by clients from 10+ IIS servers using connection pooling (we have aprox 7-10k batch/sec) in production environment and we are using...
more »

2017-10-12 14:10 (2) Answers

Get Date Ranges in SQL

I have a table that look something like follows Price | DateTime --------+------------- 100 | 22/01/2016 210 | 23/01/2016 110 | 24/01/2016 10 | 25/01/2016 20 | 26/01/2016 30 | 13/03/2016 40 | 14/03/2016 50 | 1...
more »

2017-10-12 13:10 (1) Answers

SQL Query based on common field

What SQL query do I need to use to get an alternative parts list based on a common reference number. The common reference in the Example below is ELS though the real table is over 100,000 rows with variable references. Table A **Reference Part...
more »

2017-10-12 12:10 (2) Answers

Fill the gaps, event based

I am trying to calculate churn of customers based on activity they could have done, opposed to churn by date that is the normal thing. We have events that is connected to a specific host, in my example all events are hosted by Alice but it could be d...
more »

2017-10-11 13:10 (2) Answers

SQL String join to table

Given few strings as SET @Codes1 = 3,4 SET @Codes2 = 1 SET @Codes3 = --empty Table -- TblCode Id Code 1 A 2 B 3 C 4 D How to convert the @Codes1, @Codes2, @Codes3 with join to the table TblCode so it returns the following out...
more »

2017-10-11 13:10 (2) Answers

Using column value as column name in subquery

I'm working with a legacy DB that has a table that houses field names from other tables. So I have this structure: Field_ID | Field_Name ********************* 1 | Col1 2 | Col2 3 | Col3 4 | Col4 and I need to pull a list of this field met...
more »

2017-10-10 20:10 (1) Answers

T-SQL column derived by two columns

Suppose to have this query: SELECT ((ColumnA*ColumnB)/ColumnC) AS ColDerA ,(ColumnD + ColumnE - ColumnF) AS ColDerB ,(ColDerA - ColDerB) AS ColDerC FROM TableA When I write (ColDerA - ColDerB) AS ColDerC SQL return this error: Invali...
more »

2017-10-10 10:10 (2) Answers

Multiple LIKE clauses SQL

I am working on a query to show patients who are on certain types of medication. I have a list of a few different meds that I'm seeking -- I am looking to see if there are any patients in the database who have a prescription for any of these medicati...
more »

2017-10-10 00:10 (1) Answers

Eliminating rows by date range

I have the following: Index dateOfInquiry 649454 2016-02-05 649455 2016-02-05 And I've got this query: SELECT COUNT(a.dateOfInquiry) as NumberRecords FROM ( SELECT ROW_NUMBER() OVER(ORDER BY dateOfInquiry ASC) ...
more »

2017-10-09 22:10 (2) Answers

SQL - Finding working days from days worked

We have a table that contains resources and the days' that they work. For example, one person could work Monday - Friday but another could only work two days of that week. Here is the data: WorkOnSunday WorkOnMonday WorkOnTuesday WorkOnWedne...
more »

2017-10-09 17:10 (1) Answers