
Now, we have a table with 100,000 rows (depends on total rows available in sys.columns) and going to count the number of spaces in “Comment” column in various ways. In my logic i have tried to generate random number of spaces in each row but you can choose your own way to do so.
Totalspaces code#
You can use any logic to create the dummy data and so you can avoid the above code which i have used to create a dummy data. + CASE WHEN ABS(CHECKSUM(NEWID())) % 3 = 0 THEN CHAR((ABS(CHECKSUM(NEWID())) % 26) + 65) ELSE ' ' END + CASE WHEN ABS(CHECKSUM(NEWID())) % 2 = 0 THEN CHAR((ABS(CHECKSUM(NEWID())) % 26) + 65) ELSE ' ' END SELECT TOP 100000 IDENTITY(INT) AS RowID,

Create Demo table with dummy data with 100,000 rows Remember, in this table i am going to put 100,000 records to compare the performance of various methods too. We will use this table for demo in this post. In case you want to share your own method, I welcome it you all to share this in comment section of this post.Ĭreate a demo table as below and then put some demo data in it.

I am not going to cover all the possible ways to solve this problem and in fact you can achieve this in some other ways too. Why i am doing this in various ways and sharing with you is just to show you the performance of different approaches of problem solving and also to share some interesting tricks to solve this problem in different manners. Here, i am sharing some of these methods. I have a string which contains spaces at multiple places and i just want to count them all.

In this post we are going to find the total number of spaces in a given string.
