sql server function

1. In sql server, you need to write sql statement to calculate the percentage:

 Let's take an example:

SELECT 'Number of full-time male teachers' as 'category',COUNT(*) as 'Number' ,
ltrim(Convert(numeric(9,2),(SELECT COUNT(*)FROM PT_TEACHER WHERE PARTTIMEJOB='full-time teacher'AND GENDER='male')*100/(SELECT COUNT(*)FROM PT_TEACHER WHERE PARTTIMEJOB='full-time teacher ') ))+'%'
as 'proportion' FROM PT_TEACHER a WHERE a.PARTTIMEJOB='full-time teacher' AND a.GENDER='male' ;

 

 Detailed explanation : ltrim(Convert(numeric(9,2),Result*100.0/tid))as "probability"

numeric(9,2), the field is character type, the length is 9, and there are 2 decimal places.

 2. DATEDIFF() function

Definition and usage: The DATEDIFF() function returns the number of days between two dates

Syntax:    DATEDIFF( datepart , startdate , enddate ), where startdate and enddate are valid date expressions.

Reference value for datepart:



 

Practical exercise:

 1. Find the exact age according to the date:

 

FLOOR(datediff(DY,BIRTHDATE,getdate())/365.25)

 

 Note : DY is the number of days obtained, BIRTHDATE is the date column in the table, and getdate() is the current time. Divide by 365.25 to get how many years.

FLOOR() rounds down to the specified decimal place. For example: floor(1.45,0) = 1; floor(1.55,0) = 1

Other functions: round() converts the original value to the specified number of decimal places following rounding,

如:round(1.45,0) = 1;round(1.55,0)=2

ceiling() rounds up to the specified number of decimal places such as: ceiling(1.45,0) = 2;ceiling(1.55,0)=2

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327000934&siteId=291194637