SQL intercepts the number of floors from the room number or from a string containing several "-" intervals, or intercepts the first few characters or numbers after the first few "-"

Source: Internet
Author: User

Code (room number)

Code

1-1-101

1-5-502

15-3-2001

According to the code room number of the intercepted floor for

Floor

1

5

20

SELECT case when Len (Code, CHARINDEX ('-', REVERSE (Code))-1))
= 3 then Left (code, CHARINDEX ('-', REVERSE (Code))-1), 1)
ELSE Left (code, CHARINDEX ('-', REVERSE (codename))-1), 2) END as Floor, *
From [00 Tao ran 16]

Right (codename, CHARINDEX ('-'), REVERSE (code name))-1) to intercept the third, that is, to intercept the last string, which is the final '-', if the length is 3, the first character is the floor, and if the length is 4, the top 2 characters are the floor.

SQL intercepts the number of floors from the room number or from a string containing several "-" intervals, or intercepts the first few characters or numbers after the first few "-"

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.