Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Len did not display right size Expand / Collapse
Author
Message
Posted Friday, December 21, 2012 2:13 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 17, 2013 6:34 AM
Points: 219, Visits: 431
I use len function to find out varchar column data size but did not display right size.
For example, in some cell of varchar column, data is "0", "6"... but return are 11

select NDC, len(NDC) as NDC_LEN from tableA

RESULT:

NDC ---NDC_LEN
----------------
0------11
6------11

I use len(ltrim(rtrim(NDC))) but got the same result.
How to fix it?
Post #1399591
Posted Friday, December 21, 2012 2:39 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:45 PM
Points: 8,567, Visits: 8,218
adonetok (12/21/2012)
I use len function to find out varchar column data size but did not display right size.
For example, in some cell of varchar column, data is "0", "6"... but return are 11

select NDC, len(NDC) as NDC_LEN from tableA

RESULT:

NDC ---NDC_LEN
----------------
0------11
6------11

I use len(ltrim(rtrim(NDC))) but got the same result.
How to fix it?


You are going to have to provide some details before we can do much to help. ddl, sample data please. Without ddl at the very least we are left to guess what the datatypes are. Please see the article at the first link in my signature for how to post a question.



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1399595
Posted Friday, December 21, 2012 3:12 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902, Visits: 26,783
adonetok (12/21/2012)
I use len function to find out varchar column data size but did not display right size.
For example, in some cell of varchar column, data is "0", "6"... but return are 11

select NDC, len(NDC) as NDC_LEN from tableA

RESULT:

NDC ---NDC_LEN
----------------
0------11
6------11

I use len(ltrim(rtrim(NDC))) but got the same result.
How to fix it?


What do you get for the following?
select NDC, len(NDC) as NDC_LEN, LEN(REPLACE(NDC,CHAR(160),CHAR(32))) As LenWithHardSpacesRemoved FROM TableA


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1399603
Posted Friday, December 21, 2012 3:16 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 17, 2013 6:34 AM
Points: 219, Visits: 431
The same result

NDC NDC_LEN LenWithHardSpacesRemoved
0 11 11
6 11 11
5 11 11
0 11 11
Post #1399608
Posted Friday, December 21, 2012 3:22 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:45 PM
Points: 8,567, Visits: 8,218
Again...what is the datatype? The best you are going to get is pure speculation unless you give us the whole picture.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1399612
Posted Friday, December 21, 2012 3:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:02 AM
Points: 37,677, Visits: 29,932
How about
SELECT '[' + NDC + ']' FROM TableA
?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1399614
Posted Tuesday, December 25, 2012 7:23 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 2,340, Visits: 3,167
Try running this against your table and see what ASCII codes fall out:

DECLARE @T TABLE (MyChar VARCHAR(11))

INSERT INTO @T
SELECT '0' + CHAR(10) + CHAR(13) + CHAR(9)
UNION ALL SELECT '6' + REPLICATE(CHAR(10) + CHAR(13) + CHAR(9), 3)
UNION ALL SELECT '8 '

;WITH Tally AS (
SELECT n=number
FROM [master].dbo.spt_values Tally
WHERE [Type] = 'P' AND Number BETWEEN 1 AND 11),
Codes AS (
SELECT MyChar, [LEN(MyChar)]=LEN(MyChar), [DATALENGTH(MyCHAR)]=DATALENGTH(MyCHAR)
,n, [ASCII]=ASCII(SUBSTRING(MyChar, n, 1))
FROM @T
CROSS APPLY (SELECT n FROM Tally WHERE n BETWEEN 1 AND LEN(MyChar)) a)
SELECT MyChar, [LEN(MyChar)], [DATALENGTH(MyCHAR)]
,[ASCII]=STUFF((
SELECT ',' + CAST([ASCII] AS VARCHAR(3))
FROM Codes b
WHERE a.MyChar = b.MyChar
ORDER BY n
FOR XML PATH('')), 1, 1, '')
FROM Codes a
GROUP BY MyChar, [LEN(MyChar)], [DATALENGTH(MyCHAR)]





No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1400121
Posted Wednesday, December 26, 2012 9:26 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:37 PM
Points: 1,319, Visits: 1,766
It really looks like the column is "char(11)" and not "varchar(11)".

Can you verify the column's data type?


SQL DBA,SQL Server MVP('07, '08, '09)
One man with courage makes a majority. Andrew Jackson
Post #1400280
Posted Wednesday, December 26, 2012 12:35 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902, Visits: 26,783
ScottPletcher (12/26/2012)
It really looks like the column is "char(11)" and not "varchar(11)".

Can you verify the column's data type?


Char(11) and Varchar(11) should make no difference when it comes to LEN. LEN ignores trailing spaces. There's something else in the data and the OP supposedly verified that its not the CHAR(160) character.

That, notwithstanding, I agree.... it would really be nice to know what the datatype of the column is.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1400333
Posted Wednesday, January 02, 2013 8:34 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, May 17, 2013 6:34 AM
Points: 219, Visits: 431
Thank you for help.
Data type is varchar(11)

I did one test:
1) make duplicate blank table
2) insert data as '00012345678' and '000123'
3) run scripts for len and got result are 11 and 6

It tells me that problem is not table itself but it is coming from data.

Question: How to find out hidden gabage space from data?
Post #1401874
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse