|
HI, i have a query which will return all the not null value for particular field.I have written query but this query retun one record which is null at visible point of view. when I check its length it showing 17(after using ltrim & rtrim also).So i check ascii value for one substring & it is 0.So kindly let me know how to omit this record from my selection in sql 2005. Thanks in advance Deba
(comments are locked)
|
|
Ideally, you shouldn't be doing that sort of thing in a query. If you have a string with a null terminator as it's first character, that should have been sorted out before you inserted the record. That having been said, you can just alter your query to avoid returning that row: +1 for this. However, I wonder about two WHERE-conditions AND Aren't they checking the same thing? If SOME_STRING='' it would imply LEN(SOME_STRING)=0 Also if LEN(SOME_STRING)=0 AND SOME_STRING IS NOT NULL, would imply SOME_STRING='' So I find LEN(SOME_STRING)>0 redundant. Am I right?
Jan 21 '11 at 02:04 AM
Magnus Ahlkvist
@Magnus - I think you are, yes. I just added the last bit for checking that it wasn't a C-style null terminator in the first character position, and didn't really think about the other predicates.
Jan 21 '11 at 03:03 AM
Matt Whitfield ♦♦
(comments are locked)
|
|
Matt is right to warn about letting CHAR(0) anywhere near SQL Server. SQL Server doesn't really cope with a CHAR(0) very well Most string functions can't 'see' it, even though it won't terminate the string at that point. I suspect that the Sybase programmers were C programmers and Microsoft haven't got around to pulling out the bugs that were put in then. However, LTRIM and RTRIM work as you would expect, which is to take of just the leading or trailing spaces. This means that if you have a CHAR(0) in amongst leading or trailing spaces, it will look as if LTRIM or RTIM aren't working! I hope that this little test-rig and the 'brute-force' solution in the WHERE clause helps with the problem as I'm not clear if you want to truncate your strings at the CHAR(0) as in C, or to ignore the CHAR(0)s. I've ignored them Awesome answer sir, +1
Jan 21 '11 at 11:23 AM
Matt Whitfield ♦♦
(comments are locked)
|
|
You do not need to trim the expression Books online:
Try this code, it's basically the same code as yours and tell us the result. When I use the code, it doesn't return the NULL values I am not able to under stand what you want to say.can you explain or give me syntax to omit those type of records.
Jan 20 '11 at 11:34 PM
user-806
You should be OK doing: You say "So i check ascii value for one substring & it is 0.". What do you mean when you say that you check ascii value for one substring? Ascii values are for characters, not for whole strings.
Jan 20 '11 at 11:39 PM
Magnus Ahlkvist
yah i have checked for one character
Jan 21 '11 at 12:41 AM
user-806
hi mahnus, your query not giving desire results
Jan 21 '11 at 12:53 AM
user-806
(comments are locked)
|


can you explain a bit more - which values of col1 are still returning?
Please correct me if this is wrong but you are saying that the above query returns a row that is 17 characters in length but none of it is visible in the results grid and the 1st character of the 17 is ASCII(0). ?
yes what ever you said is correct