Skip to content

Tag: tsql

SQL replace and insert Into

I have this table “NamesTable” in SQL Server. I want to insert and replace in a second column the corresponding name like this This is my query: The query runs and replace the selected words, but do not insert into my existing table. What I am doing wrong? Answer No idea what is the objective of y…

How to Further Group Items in a Column After Group By

I have an SQL table named “DATA” as follows: I’m attempting to return the following 3 columns: plan, planCount, totalCostPerPlan I’ve been working a query to return this and so far it looks like this: It sort of works, however, I get the data only partially grouped as follows: (The tab…

SQL query returns 0 for COUNT(*) if no rows

I just got started with SQL and joined this forum recently, thus I’m still lacking the knowledge. Please do be considerate if I’ve made any mistakes. I intend to get the data from the past n days. For each day, the query will count the total rows and return the result. However, there are no record…

Joining tables based on datetime2 predicate

How do I join tables A and B (see pic) on TripID and where TimeTriggered is between SegmentStart and SegmentEnd? I could try: However since BETWEEN is inclusive on both ends it would join alarm B to both segment 1 and 2. I could try >= AND < but it would leave out alarm C. I could try > AND

What is mean name after variable name in t-sql

I have the next query: Сan you please explain what it means instruction: Is a variable called startdate assigned a different name? But why? Answer First, I would write this as: The two are equivalent, but this is clearer in intent (and less likely to cause problems). In any case, @startdate is not a valid col…

Change all bit columns to int with default value NULL

I have a database where we would store Yes/No questions as bits. However, as the project is going to a different path, we need to change all bit data types to int with default value NULL. Trying the following query, I get object is dependent of column error. To fix this I try doing: However, the fact that the…