How to search for multiple entries in SQL Query?

When trying to search for one book title’s number of copies in every branch in my Library Database this works great: select BranchName, Title, sum(No_Of_Copies) as NumberOfCopies from Library_Branch lb join Book_Copies bc on lb.BranchId = bc.BranchId join BOOK b on bc.BookId = b.BookId WHERE Title like ‘%The Great Gatsby%’ group by lb.BranchName, b.Title But […]

Get in-between cell value from same column name

I previously asked how to get the count of in-between rows from same column values which can be seen here. The solutions provided works perfectly fine, but now I need additional extra column to get all values between START and END. Sample Table /—————————————–\ | ID ———- Event ——– UserID —-| | 1 ———– START […]

Convert date time string in dd/mm/yyyy to datetime format mm/dd/yyyy

There is an input from CSV file which is in dd/mm/yyyy format. I have to pass these date values to the stored procedure. I want to convert this to mm/dd/yyyy format before I bulkcopy it to the database table from the datatable in the c# code. Should I do this in the code or in […]

Like condition with multiple % in SQL Server

I want to search for the following string in a SP. How should I go about it? cal.[dbo].[GetNumberOfWorkingDays] I did not get expected results when I tried ‘%cal.[dbo].[GetNumberOfWorkingDays]%’. Does it work? : ‘%cal%GetNumberOfWorkingDays%’

Best way to store a friend's list of every user

I want to store a friend’s list of every user in SQL server database. I am confused should I use table for every user, or should I just use one table for everyone, or Is there any other best way (I’m going to query it frequently)? And how can I get all the friends of […]

Combine latest data based on ids

I have the data this way in my table and i am trying to get data as below. I tried using coalese but having issues getting data for the latest. Key Hazards DateTime 170021 Safety 2016-01-25 170021 Concerns 2016-01-25 170021 Abuse 2016-01-25 252098 Financial 2016-10-28 250606 Environmental 2016-10-26 359287 food,utilities 2016-08-08 409153 climate control 2016-06-24 […]

SQL to find timespan between rows based on ID

I have the following table in a SQL db (HeartbeatHistory) Timestamp | Comment | Id ———————— The comment can contain OK or ERR The Id is the Id of the thing that has that comment. I want to be able to query the table and find the durations that any given id was in an […]

SQL Result into one Row

I wrote a sql query like this. SELECT TR.TRAN_DATE, CASE WHEN tr.CR_DR=’CR’ THEN sum(TR.GL_TRN_AMT) ELSE 0 END AS CR, CASE WHEN tr.CR_DR=’DR’ THEN sum(TR.GL_TRN_AMT) ELSE 0 END AS DR FROM [COREBANKER1].[dbo].[GL_DAILY_TRN] AS TR WHERE TR.GL_CODE = ‘01010101’ AND TR.BRANCH_CODE = 1000 AND TR.TRAN_DATE BETWEEN ‘2016-11-01’ AND ‘2017-03-01’ GROUP BY TR.TRAN_DATE, TR.CR_DR, TR.[BRANCH_CODE] ORDER BY TR.TRAN_DATE […]

SQL Server 2008 R2 – How to filter a group of records with conditional logic?

I have the following dataset: Each sales order line has an item which can be found in various location areas in our warehouse (UPPER, GROUND, FLOOR). What I want is a way to evaluate each sales order line and then pick one location, based on a condition. The condition would say, if SO line contains […]

get column name and value from multivalue field in sql server

i have field like this D1=1.05,D2=1.05,D3=0.84,L1=1.12,L2=1.12,L3=1.12,L12=0.6,L22=0.6,L32=0.4800,I1=0.5,I2=0.5,I3=0.38,A=90,OF=-1 how could i make that look like this D1 D2 D3 L1 L2 L3 L12 L22 L32 I1 I2 I3 A OF 1.05 1.05 0.84 1.12 1.12 1.12 0.6 0.6 0.48 0.5 0.5 0.38 90 -1

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