Category "tsql"

Order by calculated column with alias inside case expression

I've got a problem with my order by clause when using a calculated column with an alias as below: This order by works without any problem declare @Mode int = 1

TSQL Insert into with CTE, how to do it?

I'm struggling with the problem below. I have written CTE where I calculate quantity of headcount in year,mth and cumulatively. My code: with HRdataCTE as

Combining cte with dates and table with dates between two dates

I have a few pieces of code that give me things that I need, but I need some help for put them together. I have: create table #test ( job int, dateL date) inser

Given a string path, find the leaf node

I am using this table and data where all children of a parent node are uniquely named. CREATE TABLE [dbo].[Resources]( [Node] [hierarchyid] NOT NULL, [N

Stream Analytics: Best parameters to choose for the autopause of a day wise TUMBLINGWINDOW stream job and best trigger time to set for that function

Context I have a daywise TUMBLINGWINDOW (similar to the one shown below) SELECT DATEADD(day, -1, System.Timestamp()) AS WindowStart System.Timestamp() A

how to get data grouped by ids

I have grouped data in a temporary table according to id_card. There are multiple activities for each card. I want to send 1 email for all activities happened o

Is there a way to check that none of the rows in a grouping contain a value?

I have a query that looks like this: SELECT ordDetails.OrderId FROM orders.OrderDetail ordDetails WHERE ordDetails.Class <> 'O' GROUP BY ordDetails.

How to select distinct value

I need to select all supervisor records. Our HR employee table set up like below Firstname Lastname Email Supervisor Frank Johns fjohns Taylor, Don Pat Hope ph

SQL Server - Decimal values not precisely calculated

For the below calculation, the expected result is -0.0000671. But the result of the code is -0.00006800000. declare @V_A decimal (38,11) = 99.99329,

SQL Join tables - join with last four digits of columns

I'm trying to join two tables in SQL. I want to join on the column with the last four digits of a phone number with the a column from a second column where only

Sql determine rows that dont have value

I have a table that returns results like abc yes abc no cdef no cdef no cdef no xyz yes xyz no xyz no sample query with abc as( select 'abc' as b

PK on temp table failing

I have a trigger which declares a table variable. DECLARE @TREarn_aaa table ( aaaID bigint NOT NULL PRIMARY KEY, Bbb decimal (18, 8) NULL, Ccc dec

Is there a faster way to find the order of a COLUMN?

My SQL Server table looks like this ID a_Toyota a_Mazda a_Nissan a_Kia a_Honda a_Subaru SoldCar CarOrder 1 8000 7000 6200

SQL question using PIVOT, no aggregation column

I would like to do a PIVOT on my table but I don't have aggregation column. SELECT CONVERT(date, DataHora) AS dia, CONVERT(VARCHAR(5), DataHora, 108) A

Inner join of an EXCEPT subquery on column names that do not appear in the output

I have a table of students and classes. I would like to find what classes were dropped from one semester to another (and a similar query for classes added). Stu

JSON with roots for every selected day

I am struggling with the problem with nesting root for every day (it's an element of my table). I'd like to get nested Key: value pair of day from table Day. He

SQL - Getting day for the whole week

I want to get the whole day of the week depend on the date, my query is working and getting the result that I want but when the date value is Sunday, result cha

Azure Synapse Serverless CETAS error "External table location is not valid"

I'm using Synapse Serverless Pool and get the following error trying to use CETAS Msg 15860, Level 16, State 5, Line 3 External table location path is not valid

tsql - How to convert multiples rows and columns into one row

id | acct_num | name | orderdt 1 1006A Joe Doe 1/1/2021 2 1006A Joe Doe 1/5/2021 EXPECTED OUTPUT id

SQL Server - Regex pattern match only alphanumeric characters

I have an nvarchar(50) column myCol with values like these 16-digit, alphanumeric values, starting with '0': 0b00d60b8d6cfb19, 0b00d60b8d6cfb05, 0b00d60b8d57a2b