Find Column First Letter Space In MS Sql

RAVI 1,076 Reputation points
2022-07-12T11:03:58.72+00:00

Hello

1) How To Find In SQL Table Column First Record Has Space Or Not For Example

Column1
Item1
Item2
Item3
Item4
Item5

If I run query it should show Item2

2) How To Find In SQL Table Column Which Has More Then One Space In Word For Example

Column1
Item 1
Item2
Ite m3
Item 4
Item5

If I run query it should show
Item 1
Ite m3

Need Sql Query In Old Version Support Thanks

Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-07-12T11:49:21.45+00:00

    If I run query it should show Item2

    Why, there is no space in it?

    First Record

    How do you define "first record"? data in a database don't have any natural order, so there is no "first".

    At all, that's to less on information.
    Please post table design as DDL, some sample data as DML statement and the expected result.

    0 comments No comments

  2. Tom Phillips 17,771 Reputation points
    2022-07-12T15:14:16.183+00:00

    Use CHARINDEX() https://learn.microsoft.com/en-us/sql/t-sql/functions/charindex-transact-sql?view=sql-server-ver16

    SELECT *  
    FROM table  
    WHERE CHARINDEX(' ',Column1)>0  
      
    

  3. LiHong-MSFT 10,056 Reputation points
    2022-07-21T03:30:12.707+00:00

    Hi @RAVI

    1) How To Find In SQL Table Column First Record Has Space Or Not

    CREATE TABLE #TEST1 (Column1 VARCHAR(20))  
    INSERT INTO #TEST1 VALUES ('Item1'),(' Item2'),('Item3'),(' Item4'),('Item5')  
    
    SELECT Column1 FROM #TEST1  
    WHERE SUBSTRING(Column1,1,1)=' '  
    

    2) How To Find In SQL Table Column Which Has More Then One Space In Word

    CREATE TABLE #TEST2 (Column2 VARCHAR(20))  
    INSERT INTO #TEST2 VALUES ('It em1'),('I te m2'),(' It em3'),('It e m 4'),(' Item5')  
    
    SELECT Column2 FROM #TEST2  
    WHERE LEN(REPLACE(Column2,' ','')) < LEN(Column2) - 1  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.