Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Locate Function In Sql in SQL

Locate Function In Sql in SQL

LOCATE Function in SQL

The LOCATE function in SQL returns the position of the first occurrence of a substring within a string. If the substring is not found, it returns 0.


? Syntax:

LOCATE(substring, string [, start_position])
  • substring: The part you're looking for.

  • string: The full text to search within.

  • start_position (optional): Where to start the search (default is 1).


? Example 1: Basic Usage

SELECT LOCATE('lo', 'Hello World') AS position;

Result:

position
4

? Example 2: With Starting Position

SELECT LOCATE('o', 'Hello World', 6) AS position;

Result:

position
8

? Notes:

  • Returns 0 if the substring is not found.

  • Case-sensitive in MySQL.

  • In Oracle, use INSTR() instead.

  • In SQL Server, use CHARINDEX().


Let me know if you'd like examples in other SQL dialects!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql