How can i check in MS SQL Server 2005 if a column is of a specific type - sql

I want to change the type of a column in MS SQL Server 2005, but before i change the type of that column i want to check if that column is of the type i want to change. How can i do that in SQL?
Thanxs,
Bas Hendriks.
Based on the anwser i wrote the following query that did the trick:
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'e_application'
AND COLUMN_NAME = 'txt_locked_by'
AND DATA_TYPE = 'nvarchar'
AND CHARACTER_MAXIMUM_LENGTH = 15 )
BEGIN
ALTER TABLE.....
END

You can query the INFORMATION_SCHEMA tables.
E.g.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'

you can use sp_help TableName , it also retrieve's the column's data types and other properties

Related

Getting column name in Microsoft SQL Server (c#)

I'm working in Visual Studios 2017 and I need to get the column names of a datatable named "Question" , from a database named "qst". Is there any way, or command that can get me the column name and then insert it in richTextBox1?
As variant you can use the following query to get all column names for a table
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='YourTableName'
ORDER BY ORDINAL_POSITION
If it's necessary all table names you can get from INFORMATION_SCHEMA.TABLES
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

I am able to obtain column names and data types from a database table with:
SELECT COLUMN_NAME, DATA_TYPE
FROM information_schema.columns
WHERE TABLE_NAME = 'xxx'
How can I get the column name and data type from a view, function, or stored procedure? I imagine I'd have to obtain them using the results of each but I'm unsure.
Thanks for the help
Columns for view:
SELECT * FROM sys.columns c where c.object_id = OBJECT_ID('<schema>.<view name>')
Columns for table valued function:
SELECT * FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS rc WHERE rc.TABLE_NAME = '<udf name>'
Columns for stored procedure
For SQL server 2012 and later:
SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set_for_object
(
OBJECT_ID('<shcema>.<sp name>'),
NULL
);
Taken from Retrieve column names and types of a stored procedure?. Read answers there for possible ways to do this (for example with pre 2012).

SQL Server : getting certain table names from query

Is it possible to display just certain table names in the query:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
Most of the time I would need all the table names but there are curtain situations where I need just certain row names.
When I try doing the following:
USE [WebContact]
SELECT COLUMN_NAME, ContactDateTime
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
It tell me that
Invalid column name 'ContactDateTime'
even though that is one of the row names.
Is this possible to do?
The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.
Since it is not a column there, SQL Server is going to error out saying that it is invalid.
I think what you're trying to do is add another WHERE clause to your statement:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!
Or if you want to add multiple columns...
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME]
IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!
Also see here for the case against using INFORMATION_SCHEMAS.
if ContactDateTime is a column that you are looking for in table memberEmails then you can do this
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'
The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first

Finding data type with query

I need to find which fields are of CLOB data type in table with SQL query?
I had tried the below query to fetch the data type but it is giving me error:
ORA-00942: table or view does not exist
Please suggest!!!
SELECT data_type
FROM SYS.COLUMNS
WHERE OBJECT_ID = OBJECT_ID('PS_P1_EPA_EMPLOYEE');
The data dictionary for Oracle isn't the same as for other RDBMS. If this is your own schema:
select data_type
from user_tab_columns
where column_name = 'PS_P1_EPA_EMPLOYEE'
... although that looks more like a table name, so maybe:
select column_name, data_type
from user_tab_columns
where table_name = 'PS_P1_EPA_EMPLOYEE'
You can also restrict on data_type ='CLOB'.
If it isn't in your schema, you can look in all_tab_colmns or dba_tab_columns. Documentation for all three views is here.

How can a column be renamed in results when getting columns from INFORMATION_SCHEMA?

So I have this:
CREATE PROCEDURE getBattingColumnNames
AS
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting'
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO
And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:
SELECT COLUMN_NAME.Doub AS 'DB'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = 'Batting')
and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?
You could use a case to translate the column name:
select case COLUMN_NAME
when 'Doub' then 'DB'
else COLUMN_NAME
end
from ...