I have several tables in a database. I would like to find which columns (in which tables) don’t have any values (all NULL in a column). I the example below, the result should be I don’t have any idea how to create this kind of query. Your help is most appreciated! Answer For a single column, count(ColumnName) returns the number
Tag: tsql
Generate class from database table
How can I generate a class from a SQL Server table object? I’m not talking about using some ORM. I just need to create the entities (simple class). Something like: Given some table like: Answer Set @TableName to the name of your table.
Eliminate and reduce overlapping date ranges
I have a set of date ranges consisting of both partially and fully overlapping dates, like this: Using T-SQL, I would like to create a new set of data, per user, with eliminated overlapping data, extending ranges and removing redundant data where needed, resulting in something like this: Cursors are fine if needed, but if I can do without them
Optimizing SELECT COUNT to EXISTS
I have a query to find certain customers from a table. There is an index on customerid, so the DB scans all rows with customerid = 22. Since the result is processed by checking whether the count returns zero or more than zero, how can I optimize the query? I.e. such that at the first customer row with amount <>
SQL Server VARBINARY(max) to c# byte[]
I am querying the table (one of the columns is a VARBINARY(MAX)) which returns some records. Then I save that as .dat.csv then I parse through that .dat file and get that varbinary value into a string by splitting the file based on commas. Now I need to convert this varbinary to byte array. How can I do that? Answer
Null Calculation With SQL Select Statement
I have Following Table Input Output ——————————— 12.22 2.22 If I pass Following Sql Statement: Select Input,Output,Balance=sum(Input – …
How to select only the first rows for each unique value of a column?
Let’s say I have a table of customer addresses: In the table, one customer like John Smith can have multiple addresses. I need the SELECT query for this table to return only first row found where there are duplicates in ‘CName’. For this table it should return all rows except the 3rd (or 1st – any of those two addresses
T-SQL Conditional WHERE Clause
Found a couple of similar questions here on this, but couldn’t figure out how to apply to my scenario. My function has a parameter called @IncludeBelow. Values are 0 or 1 (BIT). I have this query: If @IncludeBelow is 0, i need the query to be this: If @IncludeBelow is 1, that last line needs to be excluded. (i.e don’t
How to retrieve records for last 30 minutes in MS SQL?
I want to retrieve records for last 30 minutes in a table. How to do that? Below is my query.. select * from [Janus999DB].[dbo].[tblCustomerPlay] where DatePlayed < CURRENT_TIMESTAMP and …
Need to list all triggers in SQL Server database with table name and table’s schema
I need to list all triggers in SQL Server database with table name and table’s schema. I’m almost there with this: I just need to get the table’s schema also. Answer Here’s one way: EDIT: Commented out join to sysusers for query to work on AdventureWorks2008. EDIT 2: For SQL 2000