I am using SQL Server. The following query is working fine: select * from Users where Users.Id = 1 but when I use select * from Users where Users.Username = “User” this does not work. Error: …
Tag: tsql
SQL Cross-Schema stored procedure permission issues
Given a simplified stored procedure CREATE OR ALTER PROCEDURE [FooSchema].[Foo] AS SELECT B.*, FROM [BarSchema].[Bar] AS B WHERE […] After granting EXEC to a user on the FooSchema, but …
Foreign Key Refers the Primary Key Columns of Same Table
I have two tables like this: Table 1 – CivilOffices Table 2 – Offices I want to query all the offices with their respective parent office name. But when I tried to query this, the query returns the parent office id instead of parent office name. Here is the query I used. Here is the result Instead, I want result
How do I duplicate a column in a SQL Table?
I am using SQL Server 2014 and I have a table named Table1. Assuming I want to duplicate a column named SubCategory into a new column called SubCategory2 (with the same values as in column SubCategory)…
select rows with events related with another events in the same query column
I need to select rows with EventTypeID = 19 which does not have related EventtypeID = 21 LoggedOn exactly 4 minutes earlier for the same EmployeeID. Here’s the query bellow and some raw output: …
Converting NVARCHAR to INT after SUBSTRING using CHARINDEX and LEN
I am trying to join two tables using UI’s but the UI in one of the tables has ‘CLIENT_’ before it. When trying to remove the ‘CLIENT_’ from the string and then compare the UI’s I am getting the …
DELETE and NOT IN query performance
I have following table structure: Users UserId (primary key) UserName SomeItems SomeId(FK to Users.UserId) SomeItemId (primary key) Name Other stuff… SomeOtherItems SomeId2 (FK to Users.UserId)…
Best way to do a IIF conditional with subquery
I have a select with subquery inside like SELECT …. , (SELECT TOP 1 [DDC].[ContractedAmount] FROM @CustomersTable AS [DDC] …
Use multiple IF condition to set variable
I receive parameters to stored procedure: @BDOnly BIT , @CDOnly BIT , @PPOOnly BIT , @FBOMOnly …
Divide by zero error encountered when using group by
I’ve got the below piece of code in my query: CAST(COUNT(C.DateChanged) * 100.0 / COUNT(A.LastPassedBackToSalesDate) AS numeric(18, 2)) AS PBTSbutActived Both fields are of DateTime datatype. The …