Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to find the length of the sql variable..

Author  Topic 

Sarakumar
Posting Yak Master

108 Posts

Posted - 2009-07-20 : 10:11:04
Hai,
i have requirement, i need to find out what is the lenth of the field in the table..
say i have a column called custmername in the cust table..
if the length of the custername field is 20 i need to increase it to 50. how can i do..Do note that i dont want to find the lenghth of the value in the field..
thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-20 : 10:17:23
you want the query to change the length ?


alter table cust alter column custmername varchar(50)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Sarakumar
Posting Yak Master

108 Posts

Posted - 2009-07-20 : 10:21:14
i knew the alter command, but the thing i need to write a script in which i need to find the length of the field if the field length is < 20 i need to use the alter statement...
so for that only i need to know how to check the length of the field???


quote:
Originally posted by khtan

you want the query to change the length ?


alter table cust alter column custmername varchar(50)



KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-20 : 10:26:40
use INFORMATION_SCHEMA.COLUMNS



IF EXISTS
(
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = '<table name>'
AND COLUMN_NAME = '<column name>'
and CHARACTER_MAXIMUM_LENGTH = 20
)
BEGIN
ALTER TABLE . . .
END


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Sarakumar
Posting Yak Master

108 Posts

Posted - 2009-07-20 : 10:32:08
ok..Thanks a lot...
Madhivanan, can i use ur suggesstion to create the script?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-20 : 10:36:37
INFORMATION_SCHEMA.COLUMNS will be much easier for use in scripting compare to sp_columns.

To use sp_columns, you need to create a temp table, direct the output of sp_columns to the temp table before able to use it for scripting.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -