SQL: Date operation Note that if the function is to date with a string, there will be a problem because of the large amount of data

Source: Internet
Author: User

---1. Convert date By date character if it is VIP1 birthday, you can upload the data date for birthdays Begindeclare @NowBirthday datetime, @birthday datetime, @stat datetime, @end datetime, @statbirthday datetime, @endbirthday datetime, @thirdbirthday datetime, @firthbirthday datetime, @year int,@ month int, @day int, @str varchar, @total decimal, @vipno nvarchar (ten), @now datetimedeclare @smonth varchar (2), @sday VA Rchar (2) Set @vipno = ' 174030 ' Set @now =getdate () Select @birthday =birthday from Vip1 where [email protected] and Birthday isn't Nullif isdate (@birthday) =0 or @birthday = ' 1900-01-01 ' beginselect @birthday =vipbirthdaydate from Vipbirthdayaward where [Email protected]endset @year =year (@now) if month (@now) =1 and Month (@birthday) =12begin set @[email protected]endif month (@now) =1 and Month (@birthday) =11begin set @[email protected]endif month (@now) =2 and Month (@birthday) =12 begin set @[email protected]endset @month =month (@birthday) Set @day =day (@birthday) SET @ Smonth=cast (@month as varchar (2)) Select @smonthset @sday =cast (@Day as varchar (2)) If Len (@smonth) <2begin Set @smonth = ' 0 ' [email protected] select @smonthendif len (@sday) < 2begin Set @sday = ' 0 ' [email protected]end set @str =cast (@year as varchar (4)) + '/' [email protected]+ '/' [email  protected]+ ' 00:00:00 ' Set @NowBirthday =convert (datetime, @str)-----cast (@str as DateTime) if IsDate (@ Nowbirthday) =0begin Select @NowBirthday = ' 2014/01/01 00:00:00 ' endselect @NowBirthdayendGO---2 time plus minus operation Select DATEADD (yy , DATEDIFF (Yy,0,getdate ()), 0) begindeclare @NowBirthday datetime, @birthday datetime, @stat datetime, @end datetime,@ Statbirthday datetime, @endbirthday datetime, @thirdbirthday datetime, @firthbirthday datetime, @year int, @month int, @ Day int, @str varchar, @total decimal, @vipno nvarchar, @now datetime, @setyear datetimedeclare @smonth varchar (2), @sday varchar (2) Set @vipno = ' 174030 ' Set @now =getdate () set @[email protected]select @birthday =birthday from Vip1 where [email protected] and birthday are not nullif isdate (@birthday)=0 or @birthday = ' 1900-01-01 ' beginselect @birthday =vipbirthdaydate from Vipbirthdayaward where [email protected] Endset @year =year (@now) if month (@now) =1 and Month (@birthday) =12begin set @[email protected] set @setyear =datediff (Year,-1, @now) Select @setyearendif Month (@now) =1 and Month (@birthday) =11begin set @[email protected] set @sety Ear=datediff (year,-1, @now) Select @setyearendif Month (@now) =2 and Month (@birthday) =12 begin set @[email protected ] Set @setyear =datediff (year,-1, @now) Select @setyearendselect @birthday as ' birthday ' set @month =month (@birthday) Selec T @monthset @setyear =dateadd (year, DATEDIFF (year,0, @setyear), 0) set @setyear =dateadd (Month, @month-1, @setyear) Select @setyearset @day =day (@birthday) set @setyear =dateadd (Day, @day-1, @setyear) Select @[email protected]if IsDate (@ Nowbirthday) =0begin Select @NowBirthday =dateadd (yy, DATEDIFF (Yy,0,getdate ()), 0) Endselect @NowBirthdayendGO

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.