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
 Development Tools
 Other Development Tools
 Using Trim() with integer ?

Author  Topic 

Malhoosh
Starting Member

3 Posts

Posted - 2011-10-25 : 04:24:13
I'm using Visual studio 2008 to create reports.

I have a value EmployeeId which is an integer value.

I've added an expression for a table field : =trim(Fields!EmployeeId.Value)

I also made sure that "Fields!EmployeeId.Value" is seen as an integer as I've tried to multiply the value by 2, it worked fine.

My question is how come it doesn't give me an error if trim function is supposed to take strings only ?


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-25 : 04:37:31
you did trimming before multiplication right? It underwent implicit conversion to string and applied the trim and them while multiplying it again underwent implicit conversion to convert it back to int.

its analogous to below t-sql script. here also two implicit conversions are happening


declare @i int
set @i=50
select RTRIM(LTRIM(@i))*2


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Malhoosh
Starting Member

3 Posts

Posted - 2011-10-25 : 06:04:13
Thank you for your reply,

Yes that solved the problem, I did not know that Trim() does implicit conversion, I thought it should give an error if I applied it on an int.

Thank you again
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-10-25 : 06:08:25
Most functions will attempt an implicit conversion and only fail if that conversion fails.
It's common when using date functions against strings.

My opinion is that it shouldn't and you should always explicitly convert. It would make code a lot more involved but would save errors - and especially inefficient joins when the optimiser decides that it needs to convert one column and so can't use the index.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Malhoosh
Starting Member

3 Posts

Posted - 2011-10-25 : 08:00:20
Yes, I agree with you, explicit conversion will be more efficient.

Thank you for your reply.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-26 : 01:01:19
quote:
Originally posted by Malhoosh

Thank you for your reply,

Yes that solved the problem, I did not know that Trim() does implicit conversion, I thought it should give an error if I applied it on an int.

Thank you again


depends on whether its compatible datatype. if it can it will undergo implicit conversion. if not it will throw error.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -