Query Help

  • Hi All,

    I want only particular columns data from a table whose datatype is same

    i.e  I want the data for columns with datatype int or varchar or any other datatype.

    Ex: Table1 has 6 columns in which Col1,col3 are int and other are varchar datatype.

    If I pass 'int' as a variable to the query, my final Query need to be like

    Select Col1,Col3 from table1

    If I pass 'varchar' as a variable to the query, my final Query need to be like

    Select Col2,Col4,Col5,Col6 from table1

     ---------------------------------------

    I am able to get the o/p using following method.

    declare @Column_name varchar(1000)

    Set @Column_name =''

    declare @Sql varchar(1000)

    select  @column_name = @column_name + ',' +  column_name  from  INFORMATION_SCHEMA.COLUMNS where data_type='int' and table_name ='table1'

    set  @column_name  ='Select ' + Right(@column_name,len(@column_name )-1)  + ' from orders'

    print  @column_name 

    exec (@Column_name )

     ------------------------------------------

    However, is there any other way to get the same o/p in single query?

     

    Regards,

    Ramesh K

  • Looks to me like you've got a great solution!

    Steve

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply