Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:11:10+00:00 2026-06-04T09:11:10+00:00

For example I want to do something like this : //if the Column type

  • 0

For example I want to do something like this :

//if the Column type is nvarchar
//do something

//if the Column type is int
//do something

EDITED:

for example :

i have a table with columns(col1 is int,col2 is nvarchar),and i have data in this table,for example

col1 : 1,2,3,4,NULL and col2 : a,b,d,f,E,NULL

now i want to fill NULL data of col1 with 0 and NULL data of col2 with ” “

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:11:12+00:00Added an answer on June 4, 2026 at 9:11 am

    If you know the table and the column name, then:

    DECLARE @tn sysname;
    
    SELECT @tn = TYPE_NAME(system_type_id) 
    FROM sys.columns 
    WHERE name = @column_name 
    AND [object_id] = OBJECT_ID(N'dbo.tablename');
    
    IF @tn = N'nvarchar'
         DECLARE @x nvarchar(32);
    
    IF @tn = N'int'
         DECLARE @i int;
    

    However note that you won’t be able to declare the same variable name with different data types this way, even if SQL Server will only ever reach one of them. You will get something like:

    Msg 134, Level 15, State 1
    The variable name ‘@i’ has already
    been declared. Variable names must be unique within a query batch or
    stored procedure.

    If you know the name of the table then you can build a dynamic SQL statement like you propose as follows (note that this only covers a subset of types – but should give you the idea):

    DECLARE @table nvarchar(512) = N'dbo.whatever';
    
    DECLARE @sql nvarchar(max) = N'SELECT ';
    
    SELECT @sql = @sql 
        + STUFF((SELECT N',' + QUOTENAME(c.name) + N' = COALESCE(' 
        + QUOTENAME(c.name) + N',' + CASE
          WHEN t.name LIKE N'%int' THEN N'0'
          WHEN t.name LIKE N'%char' THEN N''' '''
          END + N')'
     FROM sys.types AS t
    INNER JOIN sys.columns AS c 
    ON t.system_type_id = c.system_type_id
    AND t.user_type_id = c.user_type_id
    WHERE c.[object_id] = OBJECT_ID(@table)
    ORDER BY c.column_id
    FOR XML PATH(''), 
    TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'');
    
    SET @sql = @sql + N' FROM ' + @table;
    --SET @sql = @sql + N' WHERE...'
    
    EXEC sys.sp_executesql @sql;
    

    I don’t know how you could even dream of doing this without dynamic SQL. And ugh, that’s a lot of work to prevent your presentation tier from having to deal with NULLs. That is probably the better place to deal with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ideally I want a function something like the following (this example doesn't compile): void
for example we have a table with the columns id, name, type, year. My
I have my table schema and data something like this CompanyCategoryId SubCategoryId CategoryId CompanyId
Let's say I have a table like this (this is just a simplified example,
I have following table Managers (simplified): ID, int Name, nvarchar(100) In a stored procedure
For example, I want to draw something directly to an UIView. I need an
I want to rewrite from http://example.com/blah/<something>/<somethingelse> to http://<something>.example.com/<somethingelse> , but only if the request
I have created a separate class (let's call it class2.cs for example) and want
I have a products table, with the fields product, category and cost, of type
I have a SharePoint list column of type 'Single line of text'. Out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.