73,653 questions
0
votes
0
answers
69
views
Long-running procedure being terminated before finishing
We have an End-to-End process which loads data into a Landing database. It used to run individual repopulate scripts for each data source in sequence, but that was taking more and more time to the ...
2
votes
1
answer
86
views
Way to simply list the Forms of a word by the full-text parser?
We're using fulltext indexes in current builds of SQL Server and/or Azure SQL - compatibility levels well over SQL Server 2016 or higher, preferably SQL Server 2022 on Azure.
While the searching in a ...
-4
votes
1
answer
176
views
Recursive CTE looking for all combinations of values that equal one target amount
I found examples online using a recursive CTE to find all combinations of values that equal one target amount. The database column ledger_amount is DECIMAL(26,6).
DECLARE @TARGET_AMOUNT DECIMAL(26, 6) ...
0
votes
1
answer
108
views
Aggregate function for bit-wise OR [duplicate]
In SQL Server 2022, is there an aggregate function for doing a bit-wise OR against a particular column?
-- Example table, data and output to show the different flags
DECLARE @TEST TABLE (FLAGS TINYINT)...
1
vote
2
answers
126
views
Change columns to not nullable if they're nullable
I have a group of tables being updated/replaced by an outside process. I need to add a primary key to make my queries run faster. However, if the column I'm using is nullable, it obviously won't be ...
-1
votes
1
answer
65
views
Best way to Index and or Key massive datasets [closed]
I have two tables related to each other, each with roughly 200M records.
CREATE TABLE [dbo].[AS_tblTBCDEF](
[CDEF_SOC_NUM] [numeric](5, 0) NULL,
[CDEF_EFF_DATE] [date] NULL,
[CDEF_TYP_BUS] ...
2
votes
1
answer
200
views
How to insert SM (service mark) symbol in SQL
How can I select a service mark (SM) symbol in SQL?
select CHAR(0153) --> ™
select CHAR(0174) --> ®
select CHAR(0169) --> ©
What would the equivalent be for a SM symbol?
1
vote
1
answer
58
views
Trying to convert .NET Ticks to Central Time but without the offset
In a SELECT statement I want to convert .NET ticks to a datetime and display in Central Time. I don't want the timestamp+offset that "AT TIME ZONE 'Central Standard Time'" gives, I just want ...
2
votes
2
answers
132
views
Include control characters in my XML without affecting other data
My nvarchar value contains a control character - Start of Text (0x0002 / STX). I would like to include this in my XML value. Now I know XML is not tolerant of control characters so the only way I can ...
0
votes
2
answers
182
views
For ERROR_NUMBER 515, how to get the column name from ERROR_MESSAGE
In many SQL error messages, SQL Server does not mention column name. But in messages such as the one shown below, the error message does include the column name.
Question: How can we extract the ...
3
votes
1
answer
58
views
Execute xpath having column name into a variable
I need to extract data from a XML column in SQL Server 2016, for this example I'll create a XML variable, but in the real scenario the XML in inside a table so it is not editable.
This is a sample of ...
1
vote
1
answer
81
views
SQL script to import a XML file [duplicate]
I'm trying to import an XML file into a SQL Server table, there are no errors, but all values are 0 instead of the actual values.
Could you please help me to understand what is wrong with my script?
...
1
vote
1
answer
89
views
Use the INTERSECT operator in a case-sensitive manner [duplicate]
I have a number of system-versioned tables in my database. Microsoft notes here that...
When you run any data modification queries on a temporal table, the Database Engine adds a row to the history ...
-1
votes
3
answers
162
views
Get the first Item from a "group by" without CTE or Temp Table [duplicate]
Say I have a list of orders that looks like this:
OrderId
CustomerId
ItemOrdered
OrderedWhen
1
1
Orange
2024-08-01 16:33:00
2
1
Apple
2022-01-28 01:00:00
3
1
Peach
2025-06-10 03:39:00
4
1
Banana
2022-...
0
votes
3
answers
175
views
How to combine two queries keeping all results from one
I have a table of Accounts:
AccountID
Name
1
Account A
2
Account B
3
Account C
4
Account D
5
Account E
and a table of CurrentAccounts:
AccountID
2
4
The desired result of combining these two tables is:...
0
votes
0
answers
92
views
Writing the result of a T-SQL block to a file in activebatch
Using Activebatch's JobStep library, I want to run a SELECT query in the T-SQLBlock job step, and have the output of that written to a file using the WriteTextToFile job step.
Is there actually a way ...
1
vote
1
answer
146
views
Chunk insert using Dapper of 5000 rows did not have any performance improvement over inserting row by row [duplicate]
I have an Azure Function built using .NET 8.0, which reads data from an Excel sheet and deletes all rows in Azure SQL Server, then adds around 67,000 new rows.
I was doing this insert one by one with ...
-2
votes
2
answers
149
views
Truncate a Datetime at different lengths without DATETRUNC [duplicate]
I want to truncate a datetime(7) at different lengths - determined by parts - in SQL Server 2019 (DATETRUNC starts on 2022 *). Kind of "year-month", "year-month-day", ... up to ...
1
vote
2
answers
197
views
Why ROUND() function adding too many trailing zeros
create table #t(col varchar(10))
insert into #t values(1042800)
select col, ROUND(col,2) as RoundVal from #t
update #t set col = ROUND(col,2)
The select col, ROUND(col,2) from #t returns - as ...
0
votes
1
answer
119
views
Sleeping Sessions with NULL sql_text and sql_command
I'm monitoring my SQL Server environments (local setup, various versions like SQL Server 2017, 2019, 2022) and frequently encounter sessions that appear as sleeping but have sql_text and sql_command ...
0
votes
1
answer
128
views
Remove overlapping timestamps across events with hierarchical logic in TSQL
I have source data that looks like this:
USER_ID
STATUS_GROUP
STATUS
START_TIME
END_TIME
DURATION
52a3246f6
Available
Available
2025-05-18 19:00:30
2025-05-18 19:01:13
43
52a3246f6
On_Queue
Idle
2025-...
-3
votes
1
answer
94
views
Make a batch of “Alter procedure” that are stored in a table [duplicate]
Currently I have hundreds of stored procedures that are in multiple servers, and that coexist with other stored procedures that are in other servers with linked server.
The origin of this is that when ...
0
votes
0
answers
54
views
Execute Boolean expression with sp_executesql [duplicate]
I am trying the following:
declare @V_SQL nvarchar(4000);
declare @v_formula nvarchar(4000);
declare @P_RESULT Varchar(5) ;
set @v_formula = '1 AND 0' ;
set @V_SQL =
'BEGIN
if ' + @v_formula + '
...
3
votes
1
answer
87
views
When are curly brackets required when used in a XQuery
I'm using the modify() and query() methods to interact with XML data.
Here is an example of how I'm using modify():
DECLARE @i int = 1
DECLARE @x xml = N'<?xml version="1.0" encoding=&...
3
votes
1
answer
121
views
Set rows to read-only
I have an application with the following requirements:
Before a predefined event (in my application) occurs, I need to update and insert rows in different tables.
After the event, certain rows in ...