Database practice questions 3--sql basics


1. The purpose of the exercise 1. Understand the concepts and usage of local variables and global variables;
2. Master the use of various operators;
3. Master the use of basic SELECT statements;
4. Master the flow control statements in SQL-Server Use;
5. Master the use of system functions and user-defined functions.

1. Define an int integer variable, and assign 67 and 123067 to it respectively.

declare @m int=67
print @m
declare @m int=123067
print @m

2. Define a variable length character variable with a length of 11, and assign the values ​​"Hello World!" and "How are you?" to it respectively.

declare @z varchar(11)
set @z='Hello World!'
declare @z varchar(11)
set @z='How are you?'

3. Convert the strings "WELCOME" and "student" into lowercase and uppercase letters respectively.

select LOWER ('WELCOME')
select UPPER ('student')

4. Use the RTRIM and LTRIM functions to remove the spaces on the right and left of the string "Information and Electricity Branch" respectively, and then connect with "Students' Course Selection".

select rtrim (' 信电分院 ')
select ltrim (' 信电分院 ')
select STUFF (' 信电分院 ',6,2,'学生选课')

5. Use SUBSTRING to display "CDE" in the character string "ABCDEFG".

select SUBSTRING ('ABCDEFG',3,3)

6. Use the GETDATE() function to return the current date of the system.

select GETDATE ()

7. Use the DAY() function to extract the integer of the date part of the current date.

select DAY (GETDATE())

8. Use T-SQL flow control statements to find all numbers in the Fibonacci sequence that are less than 100.

DECLARE @Q int
DECLARE @W int
DECLARE @E int
DECLARE @R varchar(2000)
set @Q =1
set @W =1
set @R=cast(@Q as varchar(10))+','+cast(@W as varchar(10))
while (@W<100)
begin 
set @E=@W
set @W=@W+@Q
set @Q=@E
if(@W<100)
set @R=@R+','+cast(@W as varchar (10))
end
print @R

*9. Use T-SQL flow control statements to find the greatest common divisor and least common multiple of two numbers.

DECLARE @a int,@b int,@c int,@d int,@x int
set @c=20
set @d=10
set @a=@c
set @b=@d
if @a<@b
begin
set @x=@b
set @b=@a
set @a=@x
end
while @b!=0
begin
set @x=@a%@b
set @a=@b
set @b=@x
end
select @a
select @c*@d/@a

Guess you like

Origin blog.csdn.net/ssdssa/article/details/108967116