In SQL, how to get the integer part and remainder part of a number

Source: Internet
Author: User
Tags getdate

Let's test how many weeks (integers) I want to get, how many days (remainder)

1. Gets the number of days between the specified date and the current date

First, use the DateDiff () function to get the number of days from the specified date to the current date

-- gets the number of days from the specified date to the current date Select DATEDIFF (DD,'2017-08-15',GETDATE())

2. Get the returned result (currently 45 days)

3, calculate how many weeks how many days

--Get Week numberSelect  $/7--get the remaining daysSelect  $%7--How many weeks are combined into the number of daysSelect CAST( (CAST(( $/7) as varchar(4))+'Week'+ CAST(( $%7) as varchar(4))+'days') as varchar(Ten))

Results

4, after the parameterization of the example

--How to get the number of weeks of pregnancyDeclare @DayNum intDeclare @FirstNum intDeclare @SecondNum int--gets the number of days from the specified date to the current dateSet @DayNum = DATEDIFF(DD,'2017-08-15',GETDATE())--get Week number of days (integer part)Set @FirstNum = @DayNum/7--get the remaining number of days (remainder)Set @SecondNum = @DayNum%7--How many weeks are combined into the number of daysSelect CAST((CAST(@FirstNum  as varchar(5))+'Week' + CAST(@SecondNum  as varchar(5))+'days') as varchar( A)) asWeeks of gestation

In SQL, how to get the integer part and remainder part of a number

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.