This is my query: declare @t table (date1 date,date2 date,date3 date) insert into @t values (‘2019-01-01′,’2019-01-20′,’2019-02-10′) insert into @t values (null,null,’2019-02-01’) insert into @t …
Tag: sql-server
How to use an IF statement within a view to drop/use a #Temp Table?
I need to create a view, which begins with the following SQL: IF OBJECT_ID(‘tempdb..#TempTable’) Is Not null Drop Table #TempTable I am also rebuilding this #TempTable further on in the view, and …
Update in the same column from the same table
I’m trying to update a column in my table that was ignored at the initial insert based on a key and not null values in the same column. My table is a history table in a data warehouse : it consists …
How do I return values from multiple columns when the column names are based on a variable result [closed]
I have a table in SQL Server which lists dates for different stages in a journey in separate columns. I also have a column which shows the current stage of the journey. For each stage, there is a …
Maximizing query performance by joining with XML
While working on query performance optimisation, I noticed that the pattern below outperforms by a wide margin other, more obvious, ways of writing the same query. After looking at the execution plans, it appears this is due to parallelism. The table MyTable, has a clustered primary key on (Identifier, MyId, …
Multiple condition calculated column
I’m trying to figure out what’s the best approach for this: If C=”WORD”: A = 51 if B is between 0 and 2000 A = 102 if B is between 2001 and 8000 A = 204 if B is higher than 8000 …
SQL – Insert rows on missing value with previous row information
I have queried the tables in a SQL Server database that return a result set with CId (the CId can be in range from 0 to 6) with the quantities. Result: I want to insert the missing CId into result table with previous row CId -> qty, if there is no previous value in group then insert qty as 0
float to string no scientific notation: SQL Server
Have a problem with some wonky ID’s that I can’t quite figure out. The above is a result of joining two tables, where ID1 = ID2. ID1 cannot be null, however ID2 can be null. What I’m attempting to arrive at is: So I can then drop the other column. I’ve tried casting, str function, remo…
SQL server error in regards to common table expression [closed]
I have this query: if object_id(‘tempdb..#mappings’) is not null drop table #mappings; Create table #mappings ( GLEntity int, NoteOwner VARCHAR(MAX) ) INSERT INTO #mappings VALUES(351, ‘Ajax …
can you set environmental variable to round numbers in SQL Server query?
I have a bunch of calculations in a SQL Server 2012 query, kind of like: select T1_month ,a.some_value, b.value_to_compare,(select (some_value – value_to_compare)/value_to_compare*100 where …