A statement that gets the number portion of a string in SQL _mssql

Source: Internet
Author: User
Copy Code code as follows:

The CREATE FUNCTION dbo. F_get_no
(
@No varchar (100)
)
RETURNS bigint
As
BEGIN
While PATINDEX ('%[^0-9]% ', @No) >0
BEGIN
SET @No =stuff (@No, PATINDEX ('%[^0-9]% ', @No), 1, ')--delete a non-numeric character, the loop ends, and the remainder is part of a number
End
Return CONVERT (bigint, @No)
End

This function can return the number part of a string
Patindex function returns the index of the first occurrence of the content in the string
The stuff function deletes the specified length of characters and inserts another set of characters at the specified starting point.
Select F_get_no (' sdsdf2334sfsd234 ') returns ' 2334234 '
Note: This method can solve the query of a field in the number of fixed values of the record, the letter part of the value is similar
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.