title: I am new to SQL Server. I created a function which action like charindex
body: <p>I am new to SQL server. I am trying to create a scalar-valued function which will act like charindex.</p>
  <pre><code>IF EXISTS(SELET * FROM sysobjects 
            WHERE id = OBJECT_ID(N'My_search_string')
              AND xtype IN (N'FN', N'IF', N'TF'))
      DROP FUNCTION My_Search_String
  GO
  
  CREATE FUNCTION My_Search_String
      (@target_string varchar,
       @string varchar)
  RETURNS INT 
  AS
  BEGIN
      DECLARE @result int;
      SET @result = CHARINDEX(@target_string, @string)
      RETURN @result 
  END
  GO
  </code></pre>
  <p>but I am not so sure why when I call the function like</p>
  <pre><code>select dbo.My_Search_string('cd','abcde')
  go
  </code></pre>
  <p>it return <code>0</code> as result.
  Thank you so much for your help :)
  have a great day guys</p>