Skip to content

Tag: tsql

Remove overlapping date intervals

I have two tables in my sql server database : PlannedPeriods : containing periods of future events UnavailabledPeriods : list or periods that are not available Here’s an example for structure and data : I would like to remove all unavailable periods from the planned periods in order to obtain this result : Answer In this case you should be

SQL STUFF FOR XML with specific grouping

I’m working in SQL Server 2014, and I have the following simple data, which tracks calling history of users: I need to group the ACTIVITY values into one field, which effortlessly be done using the STUFF FOR XML function: However I need to conditionally group these, on the condition that there was more than a 90 day between the activity

Is my understanding of how FIELDQUOTE works correct?

I am using Azure Synapse and I m querying the following CSV file: Here is the T-SQL query: Here is the output: I then altered the CSV file to add a colon just before the letter G as so: I get: This lead me to make the following conclusions about FIELDQUOTE: The query as a whole scans each row, character

Creating a Data Dictionary with example data (SQL)

I am looking to run a script on a SQL database to create a Data Dictionary with an example of the data for each field. To keep it simple I would just like to include the data from the first row of each table along with each table name and column name So something like this: Table Name Field Name

How to check consecutive values in rows in TSQL?

I have the following table: And I am looking for an output like this with a consecutive available two slots. Preferably the consecutive number should be a variable like @n = 2. How to achieve this in TSQL? Thanks! Update 8/3/2022: I undeleted this question as it is not fair to @lptr, as he/she has the correct solution, but didn’t

TSQL – New column value based on other columns with highest level of match

So I’ve got a mapping table with the following information: And another fact table with the following info: Ideally I should add CCode in the fact table with the highest matching value of Number (first) & FCODE (second) So for example the first record: Number: 0123456789 FCode: 12345 should result to have value CCode 6 to be added in the

Sybase select value from XML field

I have a table named data_values stored in SYBASE in the following format: The value field has the following XML format: I want to select from the value field values like value1, value2, etc. but I can’t find a correct query to do this. I have tried: also value is a reserved keyword I think and I need to escape

TSQL “where … group by …” issue that needs solution like “having …”

I have 3 sub-tables of different formats joined together with unions if this affects anything into full-table. There I have columns “location”, “amount” and “time”. Then to keep generality for my later needs I union full-table with location-table that has all possible “location” values and other fields are null into master-table. I query master-table, However some “location” values are dropped