0

How to convert int to varchar in SQL Server without deleting zero value?

For example this query

Select CONVERT(varchar,002000089) 

results in:

2000089 

How can I get 002000089 as the query result?

4
  • 3
    002000089 isn't an int, an int can't have preceding zero's. So you're trying to convert a string to a string. Commented Nov 23, 2020 at 10:22
  • Which dbms are you using? (That query is product specific.) Commented Nov 23, 2020 at 10:29
  • i am using sql server Commented Nov 23, 2020 at 10:30
  • 1
    Pick the right data type for your data. If you have something in which leading zeroes are relevant, then its a string of digits, not a number. Don't ever get it near an int variable/column, which is designed for storing numbers. Commented Nov 23, 2020 at 10:57

2 Answers 2

1

You have to left pad them. A pretty simple method is to use format():

select format(002000089, '000000000') 
Sign up to request clarification or add additional context in comments.

Comments

0

I have two way to get the result 002000089 in sql-server

  1. Append the converted result: 00 + CONVERT(varchar,002000089)
declare @intValue int=002000089 select '00'+ convert(varchar,@intValue) 
  1. I attach the converted result to my meta varchar value as 000000000 by stuff
declare @intValue int=002000089 declare @metaValue varchar(20)='000000000' print stuff( @metaValue, len(@metaValue)-len(@intValue)+1, len(@intValue), cast(@intValue as varchar)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.