易截截图软件、单文件、免安装、纯绿色、仅160KB

简单但有用的SQL脚本

行列转换
create table test(id int,name varchar(20),quarter int,profile int)
insert into test values(1,'a',1,1000)
insert into test values(1,'a',2,2000)
insert into test values(1,'a',3,4000)
insert into test values(1,'a',4,5000)
insert into test values(2,'b',1,3000)
insert into test values(2,'b',2,3500)
insert into test values(2,'b',3,4200)
insert into test values(2,'b',4,5500)
select * from test
--行转列
select id,name,
[1] as "一季度",
[2] as "二季度",
[3] as "三季度",
[4] as "四季度",
[5] as "5"
from
test
pivot
(
sum(profile)
for quarter in
([1],[2],[3],[4],[5])
)
as pvt
create table test2(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)
insert into test2 values(1,'a',1000,2000,4000,5000)
insert into test2 values(2,'b',3000,3500,4200,5500)
select * from test2
--列转行
select id,name,quarter,profile
from
test2
unpivot
(
profile
for quarter in
([Q1],[Q2],[Q3],[Q4])
)
as unpvt

sql替换字符串 substring replace
--例子1:
update tbPersonalInfo set TrueName = replace(TrueName,substring(TrueName,2,4),'**') where ID = 1
--例子2:
update tbPersonalInfo set Mobile = replace(Mobile,substring(Mobile,4,11),'********') where ID = 1
--例子3:
update tbPersonalInfo set Email = replace(Email,'chinamobile','******') where ID = 1

SQL查询一个表内相同纪录 having
//如果一个ID可以区分的话,可以这么写
select * from 表 where ID in (
select ID from 表 group by ID having sum(1)>1)
//如果几个ID才能区分的话,可以这么写
select * from 表 where ID1+ID2+ID3 in
(select ID1+ID2+ID3 from 表 group by ID1,ID2,ID3 having sum(1)>1)
//其他回答:数据表是zy_bho,想找出ZYH字段名相同的记录
//方法1:
SELECT *from zy_bho a WHERE EXISTS
(SELECT 1 from zy_bho WHERE [PK] <> a.[PK] AND ZYH = a.ZYH)

//方法2:
select a.* from zy_bho a join zy_bho b
on (a.[pk]<>b.[pk] and a.zyh=b.zyh)

//方法3:
select * from zy_bbo where zyh in
(select zyh from zy_bbo group b


相关文档:

Sql Server基本函数

1.字符串函数
长度与分析用
datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格
substring(expression,start,length) 不多说了,取子串
right(char_expr,int_expr) 返回字符串右边int_expr个字符
字符操作类
upper(char_expr) 转为大写
lower(char_expr) 转为小写
space(int_expr) 生成int_expr个空格 ......

SQL SERVER 2008的数据压缩


一、数据库版本
数据压缩在Sql Server 2008上才支持,2005不行,并且还要是企业版。我常常忘了这一点,在2005的Studio上闹出语法错误的状况,折腾浪费了好一阵才醒悟过来。
二、压缩状况
大约可以节省20%-50%的空间,并且行压缩和页压缩有所区别。
但让我失望的是,像含有Varchar(max),xml这种字段类型的,反而似乎压 ......

sql server 2000卸载后,再次安装的问题

错误提示,有一个文件已经挂起,必须重新启动计算机。重新启动,再次安装仍然是同样的问题。
注册表键值的问题。解决方法:
只要删除\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\下的PendingFileRenameOperations键值,就可以重新安装了。
删除后安装,不必重新启动计算机。 ......

SQL Server日期

---一个月的第一天
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 
--- 本周的星期一
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
---一年的第一天
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
----季度的第一天
SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
----上个月的最后一天
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号