I have my code but the @NAME is an optional field for searching. If the @NAME is blank or NULL, it should show all records. How do I approach this? Thanks! Answer There are some ways to do it the easy way. I would do something like this. You can use the Function ISNULL to handle blank or NULL. With
Tag: tsql
Check if procedure exists in any database SQL Server
I’d like to know if there is any way to search for a procedure in all the databases of a server (there are only SQL Server databases on that server). So far I’ve only found how to find a stored procedure in a certain database with Object_id(), but it would take too long for me to manually search e…
Compare table column with a range of values and take the position of that value from another table
I got stuck with this problem, please help if possible. Suppose we have two tables, Table A and Table B. Table A looks like this: And Table B looks like this: In table B we have around 23 values for each date, now we need to match val1 of table A to val2 of tableB for each date and return
How to group by two columns in different CASE clauses
I have a query that’s giving me the results I want, but for each item with a given ID_UNIDAD_EXPERIMENTAL there are two rows, one with column “Alt” with a value and column “Dap” with null, and the other one with “Alt” null and “Dap” with a value. My questi…
How to update several tables with a result query?
I am working with SQL Server 2017, and I need to clean up duplicate rows and update all rows in other tables that contain my field. I’ve got one table which contains my customers Then I have 7 tables that contains the userid column and 1 table with another name column My other tables: I want to clean th…
SQL help – Trying to tag values which complement the same number with opposite sign
I am trying to flag the numbers in a column which has both positive and negative numbers and adds to zero if summed up. I would like to do this by partitioning one table into multiple chunks based on Account column in the table. I have posted a picture to further explain how my result (Flag column) is expecte…
Sql Query To Sum Amount Before the Date And After Between Dates
I am looking for a SQL query to sum the amount before the date and after between dates. I have the following table in my database. Now what I want to find out is the total sum (DebitAmount) – Sum (CreditAmount) that is before 2021 and then from 2021-01-01 to 2021-03-01: Table Script The Query I Have tri…
Arithmetic Overflow Error if Varchar is over 999, WHY?
I discovered a weird issue in my database, I was able to fix it, but I do not understand WHY the error occurred in the first place. I’m using Microsoft SQL Server 2017. The following code returns an Arithmetic Overflow error: Returns Error: Arithmetic overflow error converting varchar to data type numer…
Delete all but the newest with specific filename
I migrated a file logger to log to the database instead of to disk. This caused many duplicates in the database, whereas on disk the file would have just gotten overwritten. I want to delete all the “not newest” rows given a specific filename, what SQL would do this given the following table: The …
Accurate DateDiff
I have 2 time fields in SQL. I want to calculate the difference between start time and end time in hours. From 7 AM in the morning to 7:30 PM it would be 12.5 hours but the datediff function returns 12 hours because it returns an integer. I also tried converting it to seconds: All of these return 12 hours