Skip to content
Advertisement

Tag: sql-server

Grouping rows over common data in a column and min max start/end date via cursor fetch method

I have a stored procedure with a couple of input parameters Input parameters: Result set: AuxTable is not the final intended table, it’s just being displayed for debugging/troubleshooting. MsgLinkId MyRowId WO_ID SourceUnitName MaterialName BatchID UnitofMeasure Segment1 Segment2 Segment3 Segment4 Segment5 Amount StartDate EndDate 5 1 731000037 B1S01C03 RAW MILK 0 KGM B1S01W32 NONE NONE NONE NONE 13391.7524414063 2022-01-01 14:22:25.773 2022-01-01

Dates returned as columns in SQL Select

My user will submit a FromDate and a ToDate. What I want to happen is to select the dates that fall in between these dates, which I have accomplished with the script below. The dates will by dynamic. This returns that dates as rows. What I am looking for is a way to make these dates return as the columns

Filter an XML column in SQL Server

I have an xml type column productsXML in product table I want to find all the rows that have <products></products>. I tried this: This is returning all the rows that have products tag and understandably so. Is there a way to filter only those rows that have <products></products>? Answer PLease try the following solution. It is checking that the root

SSMS SQL Identify purchases who only buy one specific item

I want to find customers who have only bought fruit in their purchase. My data looks like this: I want this: Thinking I need a where clause, grouped by ID and Purchase_date? Answer You can try to use Having with condition aggregate function to write the logic. Buy fruit (COUNT with fruit count will be greater than 1 ) Din’t

Dynamic SQL – Use declared VARCHAR in SET SQL string

How to use the declared variable @CodeID inside the SQL string? When I run following statement I get the “Invalid object name (..)” error. Answer It looks to me that you need two levels of dynamic SQL, with the first level inserting the database name (from #folders), and the second level inserting a constructed table name (based on the CodeType

SQL Query group by for a beginner

I’m quite new to SQL Server and was wondering if it was possible to run a query like this. I have a table with the following fields: USER, WORKSTATION, DURATION (seconds) Each USER can use multiple workstations and each workstation can be used by multiple USERs. In the table I have records like: I would like a result that groups

Find Multiple Substring from a String Using SQL

I have a column log. I need to extract values from that log column and make 4 new columns My table: Output I need: Answer If the string really always follows exactly that format, you can use something funky like this: Which gives: This abuses the PARSENAME function, which is meant to parse 4 part named objects (server_name.database_name.schema_name.object_name).

SQL Query to merge several views into one

I was trying to create a new view out of 3 or 4 other views/tables. TableA: title_id homeTeam 1234 WSV 5678 SSV 7890 NULL 4321 SCC TableB: title_id awayTeam 1234 SSV 5678 SFV 7890 NULL 4321 KFC TableC: title_id homeTeam 1234 SSV 5678 NULL 7890 AAB 4711 BFG I would like to generate a new view out of those three

Advertisement