Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 September SQL Puzzle: Write a Function to count the total number of word

SQL Puzzle: Write a Function to count the total number of word

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Create a user defined function to count the total number of a word from the given string data.

Sample function execution:

1
select [dbo].[fn_GetWordCount]('I am Database Engineer and I am the owner of dbrnd.com') AS TotalWordCount

Expected Output:

1
2
3
TotalWordCount
--------------
11

User defined function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CREATE FUNCTION [dbo].[fn_GetWordCount] (@InputStringData VARCHAR(100))
RETURNS INT
AS
BEGIN
 
DECLARE @i INT
DECLARE @Char CHAR(1)
DECLARE @PrevChar CHAR(1)
DECLARE @Counter INT
 
SET @i = 1
SET @Counter = 0
 
WHILE @i <= LEN(@InputStringData)
BEGIN
SET @Char = SUBSTRING(@InputStringData, @i, 1)
SET @PrevChar = CASE WHEN @i = 1 THEN ' '
ELSE SUBSTRING(@InputStringData, @i - 1, 1)
END
IF @PrevChar = ' ' AND @Char != ' '
SET @Counter = @Counter + 1
SET @i = @i + 1
END
 
RETURN @Counter
END
GO

Please try the different solution for this puzzle and share it via comment...

Sep 11, 2017Anvesh Patel
Greenplum: Important Queries to Configure the Resource Queue (Workload Management)SQL Server: BULK INSERT for insert data from Flat file (CSV) to Table
Comments: 4
  1. Dinesh
    September 12, 2017 at 7:33 am

    –Method-1

    Declare @XML As XML;
    Set @XML=”+Replace(‘I am Database Engineer and I am the owner of dbrnd.com’,’ ‘,”)+”
    Select Count(r.value(‘.’,’NVarChar(Max)’)) As TotalWordCount
    From @XML.nodes(‘t’) As Records(r)

    GO

    –Method-2

    Declare @SQL NvarChar(Max)=’I am Database Engineer and I am the owner of dbrnd.com’
    Declare @Len As Int
    Declare @Cnt Int
    Set @Cnt=0
    While Len(@SQL)>0
    Begin

    Set @Len=(Case CharIndex(‘ ‘,@SQL) When 0 Then Len(@SQL) Else CharIndex(‘ ‘,@SQL)-1 End)

    Set @Cnt=@Cnt+1
    Set @SQL=(Case Len(@SQL)-@Len When 0 Then NULL Else Right(@SQL,Len(@SQL)-@Len-1) End)

    End
    Select @Cnt As TotalWordCount
    GO

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      September 12, 2017 at 7:46 am

      Thank you Dinesh!

      ReplyCancel
  2. harini
    September 18, 2017 at 7:31 am

    declare @q varchar(max) = ‘I am Database Engineer and I am the owner of dbrnd.com’;
    select len(@q) – len(replace(@q,’ ‘,”)) + 1

    ReplyCancel
  3. VISHNUAUTO
    September 22, 2017 at 6:14 am

    ALTER FUNCTION [dbo].[fn_GetWordCount] (@InputStringData VARCHAR(100))
    RETURNS INT
    AS
    BEGIN

    DECLARE @i INT
    DECLARE @Char CHAR(1)
    DECLARE @PrevChar CHAR(1)
    DECLARE @Counter INT

    declare @input_xml xml

    select @input_xml = ” + replace(@InputStringData,’ ‘,” ) + ”

    SELECT @Counter=count(x.value(‘.’,’nvarchar’))
    FROM @input_xml.nodes(‘//*:root/*:x’) y(x)
    where y.x.value(‘.’,’nvarchar(100)’) ”

    RETURN @Counter
    END
    GO

    ReplyCancel

Leave a Reply to Dinesh Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

September 11, 2017 4 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks, string
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....