sql 常用语句

学习 笔记 专栏收录该内容
6 篇文章 0 订阅

1、sql  时间 只取日期

convert(char(10),日期列,20)

 

2、查询不重复记录

 

 select distinct  列名  from searchIP 

 

3、查询不重复记录并按某列排序

select distinct max(convert(char(10),IPtime,20)) from searchIP group by IP order by max(convert(char(10),IPtime,20)) desc

 

4、日期模糊查询

select * from searchIP where  convert(varchar(10),IPtime,20) like '%2009-03-31%'

 

5、替换语句

 

update 表 set 列名=REPLACE( convert(varchar(8000),列名,8000),'被替换内容','替换内容')

 

6、日志清除

 

backup log www_miaopu_com_cn with no_log

SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
        @MaxMinutes INT,
        @NewSize INT

-- *** MAKE SURE TO CHANGE THE NEXT 3 LINES WITH YOUR CRITERIA. ***
USE     www_miaopu_com_cn             

SELECT  @LogicalFileName = 'www_miaopu_com_cn_log', 
        @MaxMinutes = 10,              
        @NewSize = 50                 

-- Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size -- in 8K pages
  FROM sysfiles
  WHERE name = @LogicalFileName

SELECT 'Original Size of ' + db_name() + ' LOG is ' +
        CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +
        CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'
  FROM sysfiles
  WHERE name = @LogicalFileName

CREATE TABLE DummyTrans
  (DummyColumn char (8000) not null)


-- Wrap log and truncate it.
DECLARE @Counter   INT,
        @StartTime DATETIME,
        @TruncLog  VARCHAR(255)
SELECT  @StartTime = GETDATE(),
        @TruncLog = 'BACKUP LOG [' + db_name() + '] WITH TRUNCATE_ONLY'
--   Try an initial shrink.
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
--   Wrap the log if necessary.
WHILE     @MaxMinutes >  DATEDIFF (mi, @StartTime, GETDATE())
  --   time has not expired
      AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)
      AND (@OriginalSize * 8 /1024) >  @NewSize
 BEGIN --   Outer loop.
    SELECT @Counter = 0
    WHILE  ((@Counter <  @OriginalSize / 16) AND (@Counter <  50000))
      BEGIN --   update
        INSERT DummyTrans VALUES ('Fill Log')
        DELETE DummyTrans
        SELECT @Counter = @Counter + 1
      END   --   update
    EXEC (@TruncLog)  --   See if a trunc of the log shrinks it.
  END   --   outer loop
SELECT 'Final Size of [' + db_name() + '] LOG is ' +
        CONVERT(VARCHAR(30),size) + ' 8K pages or ' +
        CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'
  FROM sysfiles
  WHERE name = @LogicalFileName
DROP TABLE DummyTrans
PRINT '*** Perform a full database backup ***'
SET NOCOUNT OFF

 

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值