Saturday, April 20, 2013

[T-SQL] tsql query - Count the number of spaces in a string

[T-SQL] tsql query - Count the number of spaces in a string


tsql query - Count the number of spaces in a string

Posted: 19 Jun 2012 06:45 AM PDT

How do I write a query that tells me how many spaces (...or any character for that matter) are in a particular string?Example: David H Rogers would return a 2 since there are 2 spaces.

Storing dynamic sql

Posted: 19 Apr 2013 04:09 AM PDT

DECLARE MY_CURSOR CursorFOR SELECT [DB_NAME],[SCHEMA_NAME],[TABLE_NAME],[COLUMN_NAME] FROM dbo.Data_Profile_StatsOpen My_CursorDECLARE @DB_NAME nvarchar(500),@SCHEMA_NAME nvarchar(500),@TABLE_NAME nvarchar(500),@COLUMN_NAME nvarchar(500), @QUERY NVARCHAR(MAX)DECLARE @MAX_NUMBER INT, @MIN_NUMBER INT, @NULL_COUNT INT, @BLANK_COUNT INT, @ZERO_COUNT INT,@DISTINCT_COUNT INTFetch NEXT FROM MY_CURSOR INTO @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAME While (@@FETCH_STATUS = 0)BEGINSELECT @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAMESET @QUERY='SELECT MAX('+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME+'.'+@COLUMN_NAME+') FROM '+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME--SET @MAX_NUMBER=exec sp_executesql @QUERYEXEC SP_EXECUTESQL @QUERY, N'@MAX_NUMBER NVARCHAR(255) OUTPUT', @MAX_NUMBER=@MAX_NUMBER OUTPUTSELECT @MAX_NUMBER--SET @QUERY='UPDATE [dbo].[Data_Profile_Stats] SET MAX_NUMBER='+CAST(@MAX_NUMBER AS VARCHAR(10))+' WHERE [DB_NAME]='+''''+@DB_NAME+''''+' AND SCHEMA_NAME='+''''+@SCHEMA_NAME+''''+' AND TABLE_NAME='+''''+@TABLE_NAME+''''+' AND COLUMN_NAME='+''''+@COLUMN_NAME+''''--exec sp_executesql @QUERYFETCH NEXT FROM MY_CURSOR INTO @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAME ENDCLOSE MY_CURSORDEALLOCATE MY_CURSORGOIn the above query, I am seeing the result of the EXEC SP_EXECUTESQL but not able to see the value of @MAX_NUMBER in the next lineCan someone tell me what am I doing wrong?Thanks in advance

No comments:

Post a Comment

Search This Blog