March 1, 2010 at 11:11 pm
Comments posted to this topic are about the item Counting spaces with datalength
March 1, 2010 at 11:11 pm
Interesting Question
Thanks
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 1, 2010 at 11:20 pm
Looked at the MSDN article posted before trying to answer - had to search elsewhere as the MSDN en try wasn't very illuminating 🙂
Kelsey Thornton
MBCS CITP
March 2, 2010 at 1:26 am
thanks for the question.... was really not aware of datalength function..
March 2, 2010 at 6:29 am
When your name is in the explanation, and you get it wrong, well that's just embarrassing. :unsure:
Ron Moses
-----
a haiku...
NULL is not zero
NULL is not an empty string
NULL is the unknown
March 2, 2010 at 7:01 am
Very interesting. I typically use LEN but I can see how datalength would be useful.
March 2, 2010 at 7:36 am
Great question; it also really highlights the difference between Len() and DataLength(). For fun folks should try substituting Len for DataLength.
March 2, 2010 at 7:39 am
March 2, 2010 at 12:47 pm
Good question.
I don't know whether to be more worried that 198 people so far thought datalength behaved like length or that 118 thought 11 was an even number!
Tom
March 3, 2010 at 6:43 am
-- Sorry, I get 1 for count(1) using datalength(), record 8.
-- count(1) for Len() rerturns 0
-- What am i doing wrong?
oops, nevermind...
March 3, 2010 at 7:05 am
With the given SQL, @temp is an empty table, therefore count(1) will always return 0.
March 3, 2010 at 8:46 am
benkoskysa (3/3/2010)
With the given SQL, @temp is an empty table, therefore count(1) will always return 0.
Not if you replace DATALENGTH with LEN in this query 😉 I have to try everything myself, take NOBODY's word for anything... yes, I am a trusting soul...
--Mileage may vary--
Peter Trast
Microsoft Certified ...(insert many literal strings here)
Microsoft Design Architect with Alexander Open Systems
March 3, 2010 at 9:25 am
Good catch, thank you.
It turns out that @table was empty because I used a database where the sys.master_files table was empty; therefore, no rows for the cte...
Live and learn
🙂
March 3, 2010 at 11:30 pm
benkoskysa (3/3/2010)
... I used a database where the sys.master_files table was empty...
Well, it can't be empty. At the very least you can expect 8 rows from a brand-spankin' new install, 2 rows for each of master, tempdb, msdb and model. It's likely you don't have the right permissions to this view.
Although, this suggests 2 things: 1) that there is now another reason that people who get a QotD wrong can claim it is invalid - coz the required permissions weren't specified; and 2) that many cut-n-pasters will get the correct answer for the wrong reason.
S.
March 4, 2010 at 10:27 am
Fal (3/3/2010)
benkoskysa (3/3/2010)
... I used a database where the sys.master_files table was empty...Well, it can't be empty. At the very least you can expect 8 rows from a brand-spankin' new install, 2 rows for each of master, tempdb, msdb and model. It's likely you don't have the right permissions to this view.
Although, this suggests 2 things: 1) that there is now another reason that people who get a QotD wrong can claim it is invalid - coz the required permissions weren't specified; and 2) that many cut-n-pasters will get the correct answer for the wrong reason.
S.
I just should have opted to use a different view to populate the table with 10 records. I chose the sys.master_files for 3 reasons:
1. It has 2 rows for each of the visible system databases (no rows are visible for resource), making it 8 in total
2. Usually we have at least one non-system database (if we have a job :-)), which adds 2 more records, making it at least 10 in total, which satisfies the test requirement to have 10 records needed for QoD
3. The total number of records is still pretty small so the cte part is not heavy.
I guess the better choice would be to opt for something not heavy but less restrictive, for example
select
top 10
row_number() over (order by table_name) record_number
from information_schema.tables
for the cte part. Thank you for pointing out the flaws in my question. I will try to be more accurate next time.
Oleg
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply