I’m trying to do select * where column1 = % column2 %. this is my query The error: The data types varchar and text are incompatible in the add operator. Answer You can cast the text to a varchar(max): Or, if b.Client is a text too: Notes (most of which already were commented): text is deprecated; you wa…
Tag: sql-server
Loop and insert more than one comma separated List in SQL
I wish to loop through two comma-separated values and perform an insert As an example lets consider two variables Using this loop I am able to extract @Answers and can perform insert. But I wish to extract @Qid and insert inside the loop. edit for more clarity it is a feedback module. my result table have Qid…
Sum By Criteria in specific column
I have questions about SQL Server query for SUM. I have 2 tables: 1) EmployeesAtt (EId, EName, Stats) -> I have some criteria for Stats (0 = late, 1 = overtime, 2 = normal) 2) AttLogs (ID, EId, late, overtime, normal) From EmployeesAtt table, I want to SUM the EId And Stats in dedicated column in AttLogs l…
SQL: INSERT multiple different hardcoded values in one column
I could not find a solution for this yet. I want to insert multiple rows with 2 or more different hardcoded values, but also with data that I get from another table. Example: I want to add 2 items into a table for a user that has the ID = ‘0’ in another table without running 2 queries. This is
T-SQL, looking to extract drug dose from column
I have a column of drug names with dose and I’m trying to extract the just the dose from the field. The difficulty comes in when I have combination drugs that have multiple doses. I can either extract the first numbers in the string, or all of them in one string with no way to separate them. Desired out…
Get Max And Min dates for consecutive values in T-SQL
I have a log table like below and want to simplfy it by getting min start date and max end date for consecutive Status values for each Id. I tried many window function combinations but no luck. This is what I have: This is what want to see: Answer This is a typical gaps-and-islands problem. You want to aggreg…
How to copy data from one table to another when foreign key is only defined in the destination table?
Table_A schema: Table_B schema: Column UserId is defined as Foreign Key in Table_A. I need to copy data from Table_B to Table_A and I used: But I get: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UserId” The conflict occurred in database “Table_A”, table “d…
SQL – find all combinations
From a dataset like this, I need to get all possible combinations of cases in the same room so that no case overlaps another. The expected result is I am able to get single line results of what case can be combined with which other case like: And I’ve tried some recursions but I am at a loss nowhere nea…
How do i perform a conditional update in SQL with data from multiple tables?
Trying to run an update on a column and i’m getting a syntax error on the FROM line connecting the tables Answer The WHERE clause belongs at the end of the update join statement: See SQL update query using joins for a good canonical answer to your question.
I can not make a composite relationship between 2 tables in SQL Server
I have the next schema where I want to relation the tables COMPANIES_COUNTRIES and TIME_TRACKING_REQUEST_TYPE by CountryId and CompanyId. DB Schema The scripts for both tables are the next: I tried to make a composite relationship with next script but didn’t work: I get this error (sorry, it is in Spani…