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
 Fetch Only Varchar Column

Author  Topic 

yaman
Posting Yak Master

213 Posts

Posted - 2011-01-31 : 09:24:36
Sir ,

I have one table in which 4 columns .

SNO INT
NAME VARCHAR()
SAL INT
DEPT VARCHAR()

I want to fetch only that columns who have varchar datatype .

Please help me out Sir .



Yaman

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-01-31 : 09:49:23
select NAME, DEPT from your_table


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

yaman
Posting Yak Master

213 Posts

Posted - 2011-01-31 : 10:07:12
quote:
Originally posted by webfred

select NAME, DEPT from your_table


No, you're never too old to Yak'n'Roll if you're too young to die.



Thanks Sir ,
But I need to fetch varchar col autmatically from table.

Yaman
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-01 : 03:09:51
Why do you want to do this?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-01 : 03:20:58
Do you need the column names or the data in these columns?

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable' AND DATA_TYPE = 'varchar'

- Lumbago
My blog-> www.thefirstsql.com
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-01 : 03:27:38
And here is a way to fetch the data. There might be a better way...I just pulled this together off the top of my head:
DECLARE 
@sql nvarchar(max) = 'SELECT ',
@table_name varchar(50) = 'myTable'

SELECT @sql = @sql + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table_name AND DATA_TYPE = 'varchar'
SET @sql = LEFT(@sql, len(@sql)-1) + ' FROM ' + @table_name

EXEC sp_executesql @sql


- Lumbago
My blog-> www.thefirstsql.com
Go to Top of Page

yaman
Posting Yak Master

213 Posts

Posted - 2011-02-04 : 11:06:21
Thanks Sir .

Yaman
Go to Top of Page
   

- Advertisement -