2019-04-12

SQL语句

create table MY_DATE(

title varchar(20) not null,

name varchar(20) not null,

)

select * from MY_DATE

insert into MY_DATE  select 'firstfirst','wanj' union

select 'second','wanl'

--使用UPDATE和REPLACE函数,把TITLE=’ FIRSTFIRST’的值改为’FIRST’。

update MY_DATE set title='first' where name='wanj'

select replace('firstfirst','firstfirst','first') as 替换结果

--update MY_DATE set title=replace()

--将上面的表格的第一列的数据进行倒序输出:

select reverse ('firstfirst')

select reverse ('second')

--完成下面的SQL输出

--STR1 STR2 STR3

--1.234 1.2     1.23

select str('1.234',5,3) str1,

str('1.234',5,1) str2,

str('1.234',5,2) str3

select * from MY_DATE

--编写代码完成下面的输出:

--TITLE NAME

--IRST WAN

--ECON WAN

select name,SUBSTRING(name,1,3)as name from MY_DATE

select title,SUBSTRING(title,2,4) as title from MY_DATE where name='wanj'

select title,SUBSTRING(title,2,4) as title from MY_DATE where name='wanl'

--编写SQL代码,获取当前系统时间的年、月、日,如下图:

--YEAR MONTH DAY

--2019 4 11

select year(getdate()) year,month(getdate()) month,day(getdate()) day

--使用DATEDIFF,获得1990年1月1日和2017年5月1日之间的年、月、日的天数。如下图:

--YEAR MONTH DAY

--18 220  6695

select datediff(year,'1990-01-01','2017-05-01')as year,

datediff(month,'1990-01-01','2017-05-01')as month,

datediff(day,'1990-01-01','2017-05-01')as day

--(3)给当前系统时间增加1年6个月32天

--YEAR MONTH DAY

--2020年4月11日 2019年10月11日 2019年5月13日

select DATEADD(year,1,getdate())as year,

DATEADD(month,6,getdate())as month,

DATEADD(DAY,32,getdate())as day

--使用POWER函数,将CARS中汽车的价格的平方求出来:

--汽车品牌 汽车系列 汽车价格(万) 汽车排量 备注

--探界者 SUV     278.89 2.0L NULL

--野马   轿车  1024 3.0L 这是野马跑车   

--卡罗拉 轿车 144     1.8L 丰田卡罗拉   

create table car(

汽车品牌  varchar(20) not null,

汽车系列 varchar(20) not null,

[汽车价格(万)]  decimal(8,4) not null,

汽车排量 varchar(20) not null,

备注 varchar(50)  null

)

select *from car

insert car select '探界者','SUV','278.89','2.0L','null' union

select '野马','轿车','1024','3.0L','这是野马跑车  ' union

select '卡罗拉','轿车','144','1.8L','丰田卡罗拉 '

select power([汽车价格(万)],2)as 平方 from car

--(2)使用ROUND函数,对表(Products),输出下面的答案:

--ProductName UnitPrice

--copper       7

--gold           32

--silver      12

--(1)CAST

--Original int decimal

--9.5     9 9.5000

select  original=9.5

select cast('9.5' as int)as [int]

select cast('9.5' as decimal(1,4)) as [decimal]

--(2)CONVERT

--Original int decimal

--9.5     9 9.5000

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容