SQL query for finding number of columns - sql

I want to find the number of columns in a table but I want to find them using the LIKE condition.
Is there anyway I can do that?
Thank you very much

Assuming you are using sql server, try the following query:
SELECT COUNT(*)
FROM SYS.COLUMNS c
INNER JOIN sys.tables t ON t.object_id = c.object_id
WHERE t.name like 'TABLE_NAME'
Fill in your like condition instead of "TABLE_NAME"

Related

search for multiple columns in a single table sql

I am using SQL Server 2017. How can I search for multiple column names in a single table?
I can search for a single column name (USERID) in my database with:
select *
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%'
order by TABLE_NAME
But, I want to search for all tables that have both USERID and DOB.
Using:
select *
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%' OR COLUMN_NAME like '%DOB%'
order by TABLE_NAME
returns a list of all tables that contain the column USERID as well as all tables that contain DOB.
Changing to AND results in a completely empty list, which is correct, as a single column would not contain both terms
You can try below -
select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%USERID%' OR COLUMN_NAME like '%DOB%'
group by TABLE_NAME
having count(distinct COLUMN_NAME)=2
Below query will give you desired results:
;WITH cte
AS (SELECT object_id , ROW_NUMBER() OVER(PARTITION BY object_id
ORDER BY object_id) AS rn
FROM sys.columns AS c
WHERE c.name = 'USERID'
OR
c.name = 'DOB')
SELECT s.name + '.' + t.name
FROM sys.tables AS t INNER JOIN cte AS c ON t.object_id = c.object_id
INNER JOIN sys.schemas AS s ON t.schema_id = s.schema_id
WHERE c.rn > 1;
Please note to use exact column names with = operator and not like operator to get the correct results.

How do I dynamically join one table to multiple other tables in SQL Server?

I am currently searching my database for all column name's that have a particular substring using the below code.
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
INTO
#Tables
FROM
sys.tables AS t
INNER JOIN
sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE
c.name LIKE '%foo%'
ORDER BY
schema_name, table_name;
I want to take the table and column names this query returns and join each of them to another table like below.
SELECT *
FROM #ReferenceTable rt
INNER JOIN #Tables.table_name t ON rt.ReferenceColumn = t.ColumnName
I believe this can be done through a dynamic query, but I haven't been able to figure out how. My initial thought was to pass a table into sp_executesql, but I'm not sure that's possible. What is the correct way to join multiple table names to another table?

Need to find specific column name from database

I have one database on my server I want to filter one column name from the whole database where that column is used.
E.g: If column name is "AlternativeID" is exist in 5 tables and than I want the query that find this column name exists in which tables.?
I find the following query to find specific column name from database.
SELECT * FROM sys.columns WHERE name LIKE '%AlternativeID%'
I hope this is make sense to everyone. Any help will appreciated. Thanks!!
Try this:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%AlternativeID%'
OR...
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%AlternativeID%'
Source : Find all tables containing column with specified name
Hope this helps...
Try:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('AlternativeID')
AND TABLE_SCHEMA='YourDatabaseName';

Update data if matching value and column in all table

I have a problem on updating value in a column named 'ST_CODE' exists in many tables throughout the database.
I am able to find out the table that contain the column "ST_CODE" with the following codes.
SELECT c.name As ColNames, t.name as TableNames
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name='ST_CODE'
Is it possible to update the value from '00000' to '11000' for all the columns named 'ST_CODE' within all the tables?
Many thanks!
Yes.. create a stored procedure and insert your tablename and column name in a temporary table.
Insert into #temp(SELECT c.name As ColNames, t.name as TableNames
FROM sys.columns c JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name='ST_CODE')
Then loop your temp table and write update statement as you needed. Please follow this link for assistance. http://ask.sqlservercentral.com/questions/24259/how-to-update-a-dynamic-sql-table-with-a-set-varia.html

Joining a table onto itself in SQL and saving the result

In SQL, I am joining a table onto itself:
SELECT * FROM table AS a
LEFT JOIN table AS b
ON a.NAME = b.NAME
So it's fetching all the rows which have the same NAME appearing in a row of the other table (will also return the same row twice!).
Let's say that I want to save the result into a temporary table, say something like:
SELECT * INTO ##temp_table FROM (
SELECT * FROM table AS a
LEFT JOIN table AS b
ON a.NAME = b.NAME
)
Oh dear. SQL says:
The column 'NAME' was specified multiple times.
Reason: SQL can only create a table if every column name is unique.
Obvious solution: give every column name in the "second table" an alias.
My problem is that the actual tables I'm working with have about 40 columns. Giving every one of those columns an alias seems like a wasteful use of time. Sure, I don't need every column so could drop some of them, but deciding which ones I require just now also seems wasteful.
Question: is there a shorthand way to rename every column? For example, can I append every column name with a _2 or an _a?
Ok, you have a query, with 2 joined tables, wich returns both tables columns (i don't care if you are joining the same table with itself).
So you have two possible results
Show both colums, with differents alias (AS)
SELECT * INTO ##temp_table FROM (
SELECT a.Name AS NameA, b.Name AS NameB FROM table AS a
LEFT JOIN table AS b
ON a.NAME = b.NAME
)
Or, if you don't want them duplicated (because the other will return two times the same name)
SELECT * INTO ##temp_table FROM (
SELECT a.Name FROM table AS a
LEFT JOIN table AS b
ON a.NAME = b.NAME
)
And what if you have more colums? Ok, you can just show one of the tables in the JOIN
SELECT * INTO ##temp_table FROM (
SELECT b.* FROM table AS a
LEFT JOIN table AS b
ON a.NAME = b.NAME
)
Sorry for my bad english! I hope this can help you!
I suggest querying sys.tables and sys.columns to get your renamed fields quickly:
SELECT c.name + '_2,' ColumnName
FROM sys.columns c
JOIN sys.tables t
ON c.object_id = t.object_id
WHERE t.name = 'YourTable'
Or you can combine with the OBJECT_ID() function:
SELECT c.name + '_2,' ColumnName
FROM sys.columns c
WHERE object_id = OBJECT_ID('YourTable')