Solved

How can I find all Bit type columns in my database

Posted on 2011-09-27
8
255 Views
Last Modified: 2012-05-12
I need to find all columns which are of type Bit in my database.

What SQL query would I need to return me that information?  I am using MS SQL Server v2008.
0
Comment
Question by:FrancineTaylor
  • 4
  • 2
  • 2
8 Comments
 
LVL 5

Expert Comment

by:eridanix
ID: 36712431

USE [your_database_name]
SELECT C.TABLE_NAME, C.column_name
FROM sys.Tables T INNER JOIN
	INFORMATION_SCHEMA.COLUMNS C ON T.name = C.TABLE_NAME
WHERE C.DATA_TYPE = 'bit'

Open in new window

0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 36712443
Threw this error:

Invalid object name 'sys.Tables'
0
 
LVL 5

Expert Comment

by:eridanix
ID: 36712481
So try this:
USE [your_database_name]
SELECT C.TABLE_NAME, C.column_name
FROM information_schema.Tables T INNER JOIN
	INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME = C.TABLE_NAME
WHERE C.DATA_TYPE = 'bit'

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:FrancineTaylor
ID: 36712527
Hmm.  Still not working...

Invalid object name 'information_schema.Tables'

0
 
LVL 5

Expert Comment

by:eridanix
ID: 36712619
This can be problem with your perminiton.
Try to login as administrator if you can.

I have no problem with both this queries.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36712652
Why are you joining to anything? INFORMATION_SCHEMA.COLUMNS has the information, right?
SELECT C.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.DATA_TYPE = 'bit'

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36712685
@eridanix: remember, not everyone's database is case insensitive. INFORMATION_SCHEMA views are all uppercase. sys.* is all lowercase. I would not mix the two anyway. For newer versions of SQL, use the INFORMATION_SCHEMA views. Therefore, if you need TABLES, then it is like this:
SELECT C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
JOIN INFORMATION_SCHEMA.TABLES T 
   ON T.TABLE_SCHEMA = C.TABLE_SCHEMA
   AND T.TABLE_NAME = C.TABLE_NAME
WHERE C.DATA_TYPE = 'bit'
AND T.TABLE_TYPE = 'BASE TABLE'
;

Open in new window

0
 
LVL 5

Expert Comment

by:eridanix
ID: 36712735
@mwvisa1: Thanks for info, you are right. In my projects I use the right case format. But I'am also accustomed, that most of databases I used in past was case insensitive.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question