I want to check if an INT is null, and if so select a different name for the value. But I keep getting a Conversion failed when converting the varchar value ‘NAME’ to data type int I want something like.. Simply, if null select the second name, otherwise if it has a value, select the first Answer Use a case
Tag: sql-server
Multiple column updates in one statement – Microsoft SQL Server
I am trying to update three columns depending on three different conditions in Microsoft SQL Server. See the following example for details How would I refactor the query if I want to do it in one update statement? Appreciate your help! Answer You don’t really need the where clauses. And we can take advantage of the fact that TRY_CONVERT() will
How can I get a count for duplicates in SQL Server and write the result for each row to a column (without excluding rows)?
Suppose I have a table, like this: MyTable How can I get the number of occurrences a name appears in a given column, and then display that number to new column? For example, this would be my desired output: Instinctively, I want to use something like However, this not only omits the Number column, but it also doesn’t return ALL
SQL UPDATE on with data from same table
I have a next situation with a table, Need to update rows from rows in same table, as you see here: TABLE ID SN FID 1 12345 1 2 1122 2 3 12345-RG NULL 4 1122-RG NULL I need to UPDATE row 3 from row 1 Column FID(Same SN, different ending only on the end -RG) Update row 4 FROM
SQL – Datatypes varchar and varchar are incompatible in the modulo operator [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed last month. Improve this question I have the following query that used to work but returns the error addressed on the title. The last line is indicated within
How to extract attribute value from XML in SQL Server 2019 (v15)?
I would need to extract elements from this XML into a tabular form, but I can’t seem to get my head around how this would work on SQL Server via something like XQuery. I have all the data in a temporary table called “#1” and the XML itself lies in a field called “Message” in that temporary table. How can
How do I add text to a column that already has text in it in SQL (I want to add the new text to the end of the text that is currently stored)
I have already looked at the answers on How can I add text to SQL Column. However, I am not sure how to add the text I want to the end of the text that is already stored in the column. Can anyone help? I am using SQL Answer In supported versions of SQL Server you can employ one of
How we can loop over SQL Column with specific condition
Explanations: In 1st picture data we have. In the 2nd picture report we want to generate using SQL. In the last we have added table and data script Column Calculation = 2(Yes)/3(Total Column Contain Yes/NO) *100 Sample data: Answer You need to unpivot the CL values first and then implement a dynamic pivot: Results:
Using calculation with an an aliased column in ORDER BY
As we all know, the ORDER BY clause is processed after the SELECT clause, so a column alias in the SELECT clause can be used. However, I find that I can’t use the aliased column in a calculation in the ORDER BY clause. I know there are alternative ways of doing this particular query, and I know that this is
Between the time in SQL Server and ASP.NET
I am storing STARTTIME and ENDTIME in a SQL Server table by using ASP.NET webform. Now, as you can see in the row with timeTableID = 2, the inserted time value is 8:00 & 8:30 AM, But in the row with timeTableID = 3, the inserted time value is 8:15 & 8:25 AM, I don’t want this because its within