|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 1,162,
Visits: 728
|
|
Good back to basics question thanks, although I'm sure we've had previous on this topic.
Keep posting them though.
_____________________________________________________________________ "The difficult tasks we do immediately, the impossible takes a little longer"
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
declare @a varchar(5)='56767' select LEN(@a) It will result into 56767.
DECLARE @a VARCHAR ='xyz'
SELECT LEN(@a) AS 'Declared' , LEN(CONVERT(VARCHAR,'xyz')) AS 'Converted' , LEN(CAST('xyz' AS VARCHAR)) AS 'Cast'
When we use variable in Len method it will result us the length on basis of variable size. while in another statement LEN(CONVERT(VARCHAR,'xyz')) AS 'Converted' it is just convert the string 'xyz' into varchar, thus it result 3 select LEN(CONVERT(VARCHAR,'xyz')) AS 'Converted' -- Result 3 select LEN('xyx') -- Result 3 Same in case with CAST
Hope you get it now!! :)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 1,174,
Visits: 1,250
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
How in case of LEN(CONVERT(VARCHAR,@a)) AS 'Converted' , LEN(CAST(@a AS VARCHAR)) AS 'Cast' It is returning 30?
Can you plz exlpain.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, April 19, 2013 10:27 AM
Points: 690,
Visits: 1,100
|
|
| Nice question for Wednesday. Thanks to everyone for the discussion.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 1,174,
Visits: 1,250
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 931,
Visits: 226
|
|
Praveena-487125 (10/3/2012) Got to know that by default it takes the length as 1 if the size is not declared. Thank you....
However, I still did not get the difference between length of normal declaration and Cast/Convert... Please find below the queries....
1) SELECT LEN('xyz') AS 'Declared' , LEN(CONVERT(VARCHAR,'xyz')) AS 'Converted' , LEN(CAST('xyz' AS VARCHAR)) AS 'Cast'
Ans : 3,3,3
2) DECLARE @a VARCHAR ='xyz'
SELECT LEN(@a) AS 'Declared' , LEN(CONVERT(VARCHAR,@a)) AS 'Converted' , LEN(CAST(@a AS VARCHAR)) AS 'Cast'
Ans : 1,1,1
I did not get default of 30 (which you mentioned in the answer in case of Cast/Convert) in what ever way I try... Can you please explain more on this? You're changing the question by your examples here - it's about what the resultant variable type ends up as in each case - putting @a into each one fundamentally changes what's being asked, as does not using @a at all. It's only when you mix them as in the original question that you will get the specified behaviour.
If you explicitly declare a variable as just 'VARCHAR' it has a default length of 1 character. As such:
DECLARE @a VARCHAR = 'xyz' actually creates @a as a varchar(1) which contains only the character 'x'.
Casting or converting a string literal behaves differently and defaults to 30 characters so when you CAST('xyz' as VARCHAR) or CONVERT(VARCHAR,'xyz') you get a 30 character varchar with only three characters in it, so the 'LEN' call returns 3.
In your first version you call LEN('xyz') which is taking the length of a string literal, which is implicitly cast to a VARCHAR type so internally will be represented (someone correct me if I'm wrong here) as a VARCHAR(30) and hence the length shows as 3. It's only when you declare it as a VARCHAR explicitly that you get length of 1. That's what happens with your second version where you use the explicitly declare length 1 varchar for all your length checks.
Hope that's clearer than it feels...
Kev
------------------------------- Oh no!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
how Casting or converting a string literal behaves returning 30 characters. this is the only thing on which am stuck.. plz explain
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 6:51 AM
Points: 989,
Visits: 1,790
|
|
One comment -- the syntax of the question only works on SQL 2008 and up. This line:
DECLARE @a VARCHAR ='xyz'
is not supported on earlier versions; you have to declare and select as two steps.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 6:34 AM
Points: 1,498,
Visits: 1,509
|
|
kapil190588 (10/3/2012) how Casting or converting a string literal behaves returning 30 characters. this is the only thing on which am stuck.. plz explain
I think it has to do with "SET ANSI_PADDING ON" and the default length of varchar being 30.
Just answer off the top of my head from memory. If anyone knows better feel free to correct.
|
|
|
|