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 6775535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:55:05+00:00 2026-05-26T15:55:05+00:00

How to convert Varchar to Int in sql server 2008. i have following code

  • 0

How to convert Varchar to Int in sql server 2008.

i have following code when i tried to run it wont allowed me to convert Varchar to Int.

Select Cast([Column1] as INT) 

Column1 is of Varchar(21) NOT NULL type and i wanted to convert it into Int.
actually i am trying to insert Column1 into another table having Field as INT.
can someone please help me to convert this ?

  • 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-05-26T15:55:06+00:00Added an answer on May 26, 2026 at 3:55 pm

    Spaces will not be a problem for cast, however characters like TAB, CR or LF will appear as spaces, will not be trimmed by LTRIM or RTRIM, and will be a problem.

    For example try the following:

    declare @v1 varchar(21) = '66',
            @v2 varchar(21) = '   66   ',
            @v3 varchar(21) = '66' + char(13) + char(10),
            @v4 varchar(21) = char(9) + '66'
    
    select cast(@v1 as int)   -- ok
    select cast(@v2 as int)   -- ok
    select cast(@v3 as int)   -- error
    select cast(@v4 as int)   -- error
    

    Check your input for these characters and if you find them, use REPLACE to clean up your data.


    Per your comment, you can use REPLACE as part of your cast:

    select cast(replace(replace(@v3, char(13), ''), char(10), '') as int)
    

    If this is something that will be happening often, it would be better to clean up the data and modify the way the table is populated to remove the CR and LF before it is entered.

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

Sidebar

Related Questions

I have the following SQL: ALTER PROCEDURE [dbo].[SP_Products_GetList] @CatID int, @CatName int, @SortBy varchar(50),
select convert(varbinary(8), 1) in MS SQL Server produces output : 0x00000001 On assigning the
I need to convert the following stored procedure of SQL Server to MySQL. I
Hi i have the following stored proc in SQL Server 2005: ALTER PROCEDURE [dbo].[slot_sp_EngineerTimeslots_Group]
I want to convert below SQL Server code to VBScript in classic ASP... DECLARE
We have a stored procedure that has a select statement in it: select convert(int,
T-SQL: 1(ANSI): convert(varchar(10),@DueDate, 102) < convert(varchar(10),getdate(), 102) 2(USA): convert(varchar(10),@DueDate, 101) < convert(varchar(10),getdate(), 101) Notice
I have column with varchar values like 022008 that I need to convert into
I am developing a C# VS 2008 / SQL Server website application. I am
I have the following integer type values in a SQL script: @year , @month

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.