SQLserver User-Defined Function instance

Source: Internet
Author: User
Createfunctiondbo. t_bh (@ strvarchar (20) returnsvarchar (20) asbegindeclare @ bhvarchar (20), @ lengthint, @ iintset @ length12-LEN (@ str) ifLEN (@ str) 12beginset @ i0while @ I @ lengthbeginset @ str0 + @ strset @ I + 1endreturn @ str

Create function dbo. t_bh (@ str varchar (20) returns varchar (20) as begin declare @ bh varchar (20), @ length int, @ I int set @ length = 12-LEN (@ str) if LEN (@ str) 12 begin set @ I = 0 while @ I @ length begin set @ str = '0' + @ str set @ I = @ I + 1 end return @ str

Create function dbo. t_bh (@ str varchar (20 ))

Returns varchar (20)

As

Begin

Declare @ bh varchar (20), @ length int, @ I int

Set @ length = 12-LEN (@ str)

If LEN (@ str) <12

Begin

Set @ I = 0

While @ I <@ length

Begin

Set @ str = '0' + @ str

Set @ I = @ I + 1

End

Return @ str

End

Else if isnull (@ str, 0) = 0

Begin

Set @ str = '20140901'

Return @ str

End

Else

Begin

Return @ str

End

Return @ str

End

-- Select dbo. t_bh ('20140901 ')

, U.S. space, website space, U.S. servers

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.