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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 converting varchar to int

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-09-19 : 13:01:06
I am trying to convert varchar '0000500' to int
but, it keep truncating the front zeros.

How can I keep the front zeros,so it looks like '0000500' not '500' ?


Kristen
Test

22859 Posts

Posted - 2005-09-19 : 13:49:46
In an INT? You can't.

You could format the output to a fixed number of places, and thus have "padding" to make up the fixed width, but that's it. An INT is a number, not a formatted numeric representation.

What's the problem you are trying to csolve?

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-20 : 01:17:24
If you want to convert '0000500' to int then both 0000500 and 500 are same
Why do you need this conversion?
Where do you want to use this?

Madhivanan

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

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-09-21 : 00:20:18
0000500 is not an int.

Books Online:

quote:

int, bigint, smallint, and tinyint
Exact number data types that use integer data.

bigint

Integer (whole number) data from -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807). Storage size is 8 bytes.

int

Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647). Storage size is 4 bytes. The SQL-92 synonym for int is integer.

smallint

Integer data from -2^15 (-32,768) through 2^15 - 1 (32,767). Storage size is 2 bytes.

tinyint

Integer data from 0 through 255. Storage size is 1 byte.

Remarks
The bigint data type is supported where integer values are supported. However, bigint is intended for special cases where the integer values may exceed the range supported by the int data type. The int data type remains the primary integer data type in SQL Server.

bigint fits between smallmoney and int in the data type precedence chart.

Functions will return bigint only if the parameter expression is a bigint data type. SQL Server will not automatically promote other integer data types (tinyint, smallint, and int) to bigint.




First sentence is key.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -