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函數(shù),把TITLE=’ FIRSTFIRST’的值改為’FIRST’。
update MY_DATE set title='first' where name='wanj'
select replace('firstfirst','firstfirst','first') as 替換結(jié)果
--update MY_DATE set title=replace()
--將上面的表格的第一列的數(shù)據(jù)進(jìn)行倒序輸出:
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代碼,獲取當(dāng)前系統(tǒng)時(shí)間的年、月、日,如下圖:
--YEAR MONTH DAY
--2019 4 11
select year(getdate()) year,month(getdate()) month,day(getdate()) day
--使用DATEDIFF,獲得1990年1月1日和2017年5月1日之間的年、月、日的天數(shù)。如下圖:
--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)給當(dāng)前系統(tǒng)時(shí)間增加1年6個(gè)月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函數(shù),將CARS中汽車的價(jià)格的平方求出來:
--汽車品牌 汽車系列 汽車價(jià)格(萬) 汽車排量 備注
--探界者 SUV ? ? 278.89 2.0L NULL
--野馬 ? 轎車? 1024 3.0L 這是野馬跑車? ?
--卡羅拉 轎車 144 ? ? 1.8L 豐田卡羅拉? ?
create table car(
汽車品牌? varchar(20) not null,
汽車系列 varchar(20) not null,
[汽車價(jià)格(萬)]? 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([汽車價(jià)格(萬)],2)as 平方 from car
--(2)使用ROUND函數(shù),對(duì)表(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