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)
 Leading Zeros cut

Author  Topic 

Trent
Starting Member

39 Posts

Posted - 2005-10-10 : 15:23:39
I'm declaring a variable as char(5), yet once in a while it will take a number like '00101' and convert it to '101', sometimes it will leave it as '00101'.
Any ideas?

X002548
Not Just a Number

15586 Posts

Posted - 2005-10-10 : 16:22:09
quote:
Originally posted by Trent

yet once in a while it will take a number like '00101' and convert it to '101'



That statement is false.


DECLARE @x char(5)
SELECT @x = '00101'
SELECT @x



Read the hint link below and follow the directions in it.



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-10-10 : 16:33:28
I found that it will correctly return the leading zeros, unless the select is contained in an exec('') statement.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-10-10 : 16:35:21
Why don't you post the code..are you doing SELECT @x = 00101?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-10-11 : 05:26:44
I reckon that somewhere the value is getting cast [implicitly or otherwise] to a numeric

Perhaps in the EXEC you are processing the value in some way and NOT surrounding it with single quotes?

Kristen
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2005-10-11 : 09:19:49
RESOLVED
Stupid mistake, that's what it was.
This was part of the code in the exec statement:
Select '+@Serial+' from tSpecifics

Should have been:
Select '''+@Serial+''' from tSpecifics

Thank you all for your help
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-10-11 : 09:29:37
And why are you using dynamic SQL?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -