December 21, 2012 at 2:13 pm
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?
December 21, 2012 at 2:39 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
December 21, 2012 at 3:12 pm
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
Change is inevitable... Change for the better is not.
December 21, 2012 at 3:16 pm
The same result
NDCNDC_LEN LenWithHardSpacesRemoved
011 11
611 11
511 11
011 11
December 21, 2012 at 3:22 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
December 21, 2012 at 3:28 pm
How about
SELECT '[' + NDC + ']' FROM TableA
?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 25, 2012 at 7:23 pm
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)]
My thought question: Have you ever been told that your query runs too fast?
My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
December 26, 2012 at 9:26 am
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
December 26, 2012 at 12:35 pm
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
Change is inevitable... Change for the better is not.
January 2, 2013 at 8:34 am
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?
January 2, 2013 at 8:51 am
Would it be a problem with leading spaces (instead of trailing spaces)? Maybe hard spaces or tabs.
January 2, 2013 at 8:58 am
I use len(rtrim(ltrim(NDC))) but got the same problem
January 2, 2013 at 9:07 am
adonetok (1/2/2013)
I use len(rtrim(ltrim(NDC))) but got the same problem
Then you must have some non-display characters in your data. Try outputting select * to text instead of the grid. Do you have carriage returns? Export the data to word and show all the editing stuff. Do you see line breaks? If all else fails examine each and every character in the data and see what the ascii values are. There has to be something in there.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 2, 2013 at 5:29 pm
Sean Lange (1/2/2013)
adonetok (1/2/2013)
I use len(rtrim(ltrim(NDC))) but got the same problemThen you must have some non-display characters in your data. Try outputting select * to text instead of the grid. Do you have carriage returns? Export the data to word and show all the editing stuff. Do you see line breaks? If all else fails examine each and every character in the data and see what the ascii values are. There has to be something in there.
Did you try running the query I suggested? It should show definitively which characters are cluttering up your VARCHARs.
My thought question: Have you ever been told that your query runs too fast?
My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
January 3, 2013 at 2:26 am
Please notice that the other whitespace characters (TAB, CR, LF, with ASCII codes 9, 13, 10) do not show up in the output of most query tools. These whitespace characters are not removed by the trim functions so the length remains the same with or without a trim applied to the values. You can check whether this is the case with NDC LIKE '%[' + CHAR(9) + CHAR(13) + CHAR(10) + ']%'.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply