Summary of some common functions in SQL Server

Source: Internet
Author: User
Tags rtrim

Remove whitespace

LTRIM (' content '): Remove the space to the left of the string

RTRIM (' content '): Remove the space on the right

LTRIM (RTRIM (' content ')): Remove the left and right spaces of the string

REPLACE (' content ', ' ', '): Remove all the spaces in the string

Find replacement aspects

CHARINDEX (' characters to find ', ' What to Look for '): If you're looking for something you don't want to find, return 0, find the location where the return string is located, and the index starts at 1

Left (' string to intercept ', ' intercept length '): Intercept from

Right (' string to intercept ', ' intercept length '): Intercept from the left

PATINDEX ('%[^a-za-z.-]% ', ' content '), looking from the left, returns the location where the regular table appears in the content, not found return 0

ISNULL (' content ', '): Determine if the string is null, or return '

COALESCE (' parameter 1 ', ' Parameter 2 '): Returns parameter 1 if the value of parameter 1 is not NULL, otherwise returns the value of parameter 2

ISDATE (' content '): Determines whether it is a time format, returns 1 if it is, no words return 0

Case-When usage

SELECT time = Case ISDATE (' 2015-1-1 ')

When 1 Then ' time format is correct '

ELSE ' non-standard Time format ' END

Case-when multi-conditional judgment usage

SELECT content = Case

When the ' 1232 ' like '%1% ' then ' 1 '

When the ' 1232 ' like '%2% ' then ' 2 '

When the ' 1232 ' like '%3% ' then ' 3 '

ELSE NULL END



Case-When-embedded method

SELECT

Case CHARINDEX ('/', time 1)
When 0 then NULL

ELSE

Case
When convert (Int,left (time)) >=month (GETDATE ()) then CONVERT (VARCHAR (Ten), Year (GETDATE ())) + '-' +replace (time 1, '/', ‘-‘)

ELSE CONVERT (VARCHAR), Year (GETDATE ()) +1) + '-' +replace (time 1, '/', '-')

End end as port time,

Summary of some common functions in SQL Server

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.