SQL之乱笔--从基础开始

-----------------------日期星期的计算---------------------------------------
declare  @Date datetime=getdate()
declare  @DateString nvarchar(8)='20121225'
select  convert(nvarchar(10),@Date,126) as [Date]
	  ,CAST(@DateString as datetime) AS DateStringToDate
	  ,CONVERT(datetime,@DateString,112) AS NvarcharToDate
	  ,LEFT(@DateString,4)+'^'+substring(@DateString,5,2)+'^'+RIGHT(@DateString,2) AS OtherDateToDate
	  ,datepart(weekday,@Date)-1 AS [Weekday]
	  ,case convert(varchar,(DATEPART(weekday,@Date)-1)) 
	  when 1 then N'星期一'
	  when 2 then N'星期二'
	  when 3 then N'星期三'
	  when 4 then N'星期四'
	  when 5 then N'星期五'
	  when 6 then N'星期六'
	  when 0 then N'星期日'
	  else N'世界末日'
	  end as [weekday]
	  ,case convert(varchar(10),(DatePart(weekday,CAST(@DateString AS Datetime))-1))
	  when 1 then N'星期一'
	  when 2 then N'星期二'
	  when 3 then N'星期三'
	  when 4 then N'星期四'
	  when 5 then N'星期五'
	  when 6 then N'星期六'
	  when 0 then N'星期日'
	  else N'世界末日'
	  end as weekday
	------------------临时表的简单使用方法------------------------  

	select b.BranchName as WideBranchName ,a.BranchName
	 into #temp
	  from (
			select BranchCode,BranchName,WideBranchCode from Branch where WideBranchCode is not null
			) as a
	  join Branch as b on a.WideBranchCode=b.BranchCode
	  order by b.BranchName
	  select * from #temp
-------------------------模糊查询-----------------------------------------
select BrandCode
from Brand
where BrandCode like '%E%'
except--但不包含
select BrandCode 
from Brand
where BrandCode like '%B%'

select BrandCode
from Brand
where BrandCode like '%B%'
intersect --且包含
select BrandCode
from Brand
where BrandCode like '%E%'
--------------------字符串语句的使用------------------------------
---@sql的类型最好用 NVARCHAR(MAX)
declare @sql varchar(400)=''
set @sql='select * 
from brand 
where BrandCode like ''%B%''
union
select * 
from Brand
where BrandCode like ''%E%''
'
execute (@sql)
--------------------------------------------------
select COUNT(*),COUNT(WideBranchCode) from Branch--count统计的是不为null的记录
-------------------ROW_NUMBER的使用-----------------------------------
select row_num,BrandCode,StyleCode,Price 
into #tt 
from(

select  ROW_NUMBER() over(order by brandcode,StyleCode,Price) AS row_num 
		,BrandCode
		,StyleCode
		,Price
 from BrandPrice 
 where  enddate='99991231' and brandcode='bc'
--- order by Price desc
 
 
 ) as a 
 
-- select row_num 
-- from #tt
-- order by price desc
--fetch first 10 ROWS ONLY
 
 drop table #tt
 ---------------GROUP BY和HAVING--------------------------------
 select  brandcode, COUNT(*) as num
 from EtcPay 
 group by BrandCode
-- having COUNT(*)>6
--having COUNT(*)=6 or COUNT(*)=2
having COUNT(*) in(2,6)
 order by num desc 
 -------------------------
 select 1+2
 union
 select '1'+'2'
 
 select '''1'+'2'
 -----------------------------------查询生日是星期几--------------------------------------------------
 select top 10 Birthday 
				,CONVERT(nvarchar(8), DATEADD(MONTH,-1,Birthday),112) as a
				,case when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1) =1then N'星期一'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=2 then N'星期二'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=3 then N'星期三'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=4 then N'星期四'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=5 then N'星期五'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=6 then N'星期六'
						when (datepart(WEEKDAY,DATEADD(year,1,Birthday))-1)=0 then N'星期日'
						else 'unkown'
						end as [weekday]
 from
 Employee
 
 ---------------------------------------------------------------------------------------
select nullif(StartDate,enddate) as chkdates---nullif--比较两个值如果不等价则返回第一个值,如果相等则返回第一个的类型的空值null
from
BrandPrice
--------------------------------------------------------------------------------------

select  BrandCode,Price,COUNT(*) --over() as num-- 对于查询结果的每一行都返回所有符合条件的行的条数。
from BrandPrice
where Price>500
group by BrandCode,Price

select BrandCode,Price
		,COUNT(*) over(partition by brandcode) as num--表示对结果集按照BrandCode进行分区,并且计算当前行所属的组的聚合计算结果
		,COUNT(*) over(partition by price)
from BrandPrice
where Price>500
-------------------------------即用即释临时表用法---------------------------------------------
with tt(BrandCode,Price)
AS
(
	select BrandCode,Price  from BrandPrice where Price>=5000
	)
select * from tt where Price >7000

SELECT COUNT(1) FROM SysColumns WHERE id=OBJECT_ID('SaleDtl') --查询一个表有多少列

SELECT CASE CONVERT(VARCHAR,(DATEPART(WEEKDAY,DATEADD(YEAR,-1,GETDATE()))-1))
		 WHEN 1 THEN N'星期一'
		 WHEN 2 THEN N'星期二'
		 when 3 then N'星期三'
	     when 4 then N'星期四'
	     when 5 then N'星期五'
	     when 6 then N'星期六'
	     when 0 then N'星期日'
	     else N'世界末日'
	   END AS N'Last Year Of The Week'---去年这周是星期几

------------------------------函数和表变量--------------------------------------------

alter function GetDay
(@StartDate datetime,
@EndDate datetime)
returns  @Date table(Date Datetime,[weekday] char(1))
as
begin
	--create table #Date(Date DateTime)
	
	while @StartDate<=@EndDate
		begin
		
			insert into @Date(Date,[weekday]) values(@StartDate,convert(char(1),datepart(weekday,@StartDate)))
			set @StartDate=DATEADD(day,1,@StartDate)
			
		end
	
	return 
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值