那夜我还很年幼~
我不去想身后会不会袭来寒风冷雨 既然目标是地平线 留给世界的只能是背影

字符串操作

--字符串操作
select DATALENGTH(title) from o_youhuiquan  --字段的长度
select CONVERT(TEXT,title) from o_youhuiquan --转换成TEXT ,使一个汉字占两个字节(16位)
select SUBSTRING(title,1,9) from o_youhuiquan --字符个数截取
select SUBSTRING(CONVERT(TEXT,title),1,40) from o_youhuiquan  --按字节截取

时间操作

select DateAdd(dd,-2,intime) from o_youhuiquan where yhqid='173707'
select DateAdd(dd,-1,getdate()) --当前时间减一天

删除表里面某字段的重复记录(保留一条,多余的全删除)

首先删除一张表中可能存在的重复数据:
delete from 表 where 字段1 in
(select 字段1 from
(select 字段1,row_number() over (partition by 字段1 order by 字段2 desc) rn from 表)
where rn>1);
以上字段1为需要删除的依据字段,比如说你需要删除重复的邮箱,那么字段1表示邮箱,而字段2是按照顺序你需要保留的记录,比如说按照时间排序,保留时间最近的那个邮箱。

删除一张表中的另一个表中已经存在的记录
delete from 表1 where exists
(selete 1 from 表2 where 表1.字段=表2.字段);

--删除title重复字段
delete from o_youhuiquan where title in
(
    select title,yhqid from ( 
        select   title,yhqid,ROW_NUMBER() over (partition by title order by yhqid desc) as rm from  o_youhuiquan 
        
    ) as temp where rm>1
)


select title,yhqid from ( 
        select    title,yhqid,ROW_NUMBER() over (partition by SUBSTRING(CONVERT(TEXT,title),1,40) order by yhqid desc) as rm from  o_youhuiquan --SUBSTRING(CONVERT(TEXT,title),1,40)表示title字段的值只要前40个字节和其它字段的前40字体相等,就视这个字段有重复记录
        
    ) as temp where rm>1


elect * from o_youhuiquan where SUBSTRING(CONVERT(TEXT,title),1,40) in
(
    select SUBSTRING(CONVERT(TEXT,title),1,40) from  o_youhuiquan where title='『茶琳雅韵』大红袍 金骏眉 铁观音 龙井 碧螺春 普洱 花/果茶'
)

完毕

posted on 2013-05-29 23:42  那夜我还很年幼~  阅读(349)  评论(0)    收藏  举报