Skip to content

Tag: sql-server

How to copy parent and child data from one database to another while assigning the newly created parent ids in the foreign key column of child tables?

Suppose, I have a table tblClasses and tblStudents Now each class have multiple student. tblStudents now, I have another server having exact same db and tables and I am copying data from server 1 to server 2 from same tables using the Select and Insert e.g. and for tblStudents now this is ok but the real prob…

Get value for every date in table with date ranges

I’m working with table, its structure looks like this: from_date DATE %Y-%m-%d to_date DATE %Y-%m-%d person VARCHAR tax INT Table shows that in period between date_from and date_to for such person there was such tax The whole point is that these periods can intersect and activities can sum. Roughly spea…

Searching through different tables?

I have several tables with a Sequence as the primary key. They are unrelated at all, there are no foreign keys or anything, or anything that could link one to another, so join won’t work. Union won’t work either because they are basically different tables. I know that you could assign “null&…

Combining data into one table cell

There is a query that returns a temporary table. TSQL Query: Result: Random sampling of output values Is it possible to group data from the last column, thereby reducing the number of lines in the output. That is, I need it to be like this: Desired result UPD: SQL Server 2016. STRING_AGG is missing Answer Gro…

Power function in T SQL

The following returns .81 as expected: But this does not. Its return value is .8: To have it return .81, I have to cast the input: Why is a cast needed here? What am I missing? Answer The power function return type is dictated by the data type of the first parameter. Here, you’re giving it a decimal(1,1…