Best way to do nested case statement logic in SQL Server
You could try some sort of COALESCE trick, eg: SELECT COALESCE( CASE WHEN condition1 THEN calculation1 ELSE NULL END, CASE WHEN condition2 THEN calculation2 ELSE NULL END, etc… )
You could try some sort of COALESCE trick, eg: SELECT COALESCE( CASE WHEN condition1 THEN calculation1 ELSE NULL END, CASE WHEN condition2 THEN calculation2 ELSE NULL END, etc… )
You will need to post the table definitions for the source and destination tables for us to figure out where the issue is but the bottom line is that one of your columns in the source table is bigger than your destination columns. It could be that you are changing formats in a way you … Read more
Look at an index like this: Cols 1 2 3 ————- | | 1 | | | A |—| | | | 2 | | |—|—| | | | | | | | 1 | 9 | | B | | | | |—| | | | 2 | | | |—| | | | … Read more
You could use CAST or CONVERT: SELECT CAST(MyVarcharCol AS INT) FROM Table SELECT CONVERT(INT, MyVarcharCol) FROM Table
SELECT logcount, logUserID, maxlogtm, DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff FROM statslogsummary WHERE ( DATEDIFF(day, maxlogtm, GETDATE() > 120) Normally you can’t refer to field aliases in the WHERE clause. (Think of it as the entire SELECT including aliases, is applied after the WHERE clause.) But, as mentioned in other answers, you can force SQL to … Read more
You can put set xact_abort on before your transaction to make sure sql rolls back automatically in case of error.
You are mixing implicit joins with explicit joins. That is allowed, but you need to be aware of how to do that properly. The thing is, explicit joins (the ones that are implemented using the JOIN keyword) take precedence over implicit ones (the ‘comma’ joins, where the join condition is specified in the WHERE clause). … Read more
SQL Server 2008: SELECT cast(AttDate as time) [time] FROM yourtable Earlier versions: SELECT convert(char(5), AttDate, 108) [time] FROM yourtable
You can’t update multiple tables in one statement, however, you can use a transaction to make sure that two UPDATE statements are treated atomically. You can also batch them to avoid a round trip. BEGIN TRANSACTION; UPDATE Table1 SET Table1.LastName=”DR. XXXXXX” FROM Table1 T1, Table2 T2 WHERE T1.id = T2.id and T1.id = ‘011008’; UPDATE … Read more
— Disable the constraints on a table called tableName: ALTER TABLE tableName NOCHECK CONSTRAINT ALL — Re-enable the constraints on a table called tableName: ALTER TABLE tableName WITH CHECK CHECK CONSTRAINT ALL ——————————————————— — Disable constraints for all tables in the database: EXEC sp_msforeachtable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’ — Re-enable constraints for all … Read more