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