Home > T-SQL > Square Root – SQL Function

Square Root – SQL Function


SQRT() function returns a square root value of a float_expression. The return type of this function is float.

In general, A square root of a number a is a number y such that y2 = a. – Wikipedia

Syntax

SQRT (float_expression)

Example

\scriptstyle \sqrt{a} = SQRT(a)

T-SQL Example

select SQRT(3)
Output: 1.73205080756888
 
select sqrt(16)
Output: 4

T-SQL code to print 10 perfect square 

declare @ftsqrt as float = 1
declare @i as int = 1
declare @n as int = 10

while @i < @n
begin
if @i = sqrt(@ftsqrt)
begin
print ‘SQRT(‘ + rtrim(cast(@ftsqrt as varchar(20))) + ‘)=’
print sqrt(@ftsqrt)

set @i = @i + 1
end
set @ftsqrt = @ftsqrt + 1
end

Now you got  SQRT() function in your hand and just play with it. Thanks for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: