惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
博客园 - 【当耐特】
WordPress大学
WordPress大学
爱范儿
爱范儿
美团技术团队
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
量子位
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
博客园 - 叶小钗
GbyAI
GbyAI
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Full Disclosure
G
Google Developers Blog
D
Docker
T
Tailwind CSS Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
B
Blog
博客园 - 三生石上(FineUI控件)
博客园 - Franky
H
Help Net Security
MyScale Blog
MyScale Blog
U
Unit 42
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog

博客园 - 代码泪

VC2008下配置boost库使用正则表达式[转] 解决下载/上传大文件问题(IIS6)[转] 动态创建DataTable[转] C#格式化字符串,日期,时间,货币[转] Winform数据库连接app.config文件配置 C# 连接SQL数据库 常用连接字符串 update select DataTime.Now.Ticks精确的时间单位[转] [SqlException (0x80131904): 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。……(provider: TCP 提供程序, error: 0 - 由于目标机器积极拒绝,无法连接。)] VS 网站发布失败 [收藏]"Automation服务器不能创建对象" 的多种解决办法 Hashtable, ArrayList, List, Dictionary学习[转] VSTS for Testers学习笔记目录[转] SQL Server 2005安全配置[转] 超时时间已到.错误及Max Pool Size设置 execute sp_executesql 用变量获取返回值【转】 使用存储过程并返回值与及返回值的获得方法[转] js取得dropdownlist的text,value[转] - 代码泪 - 博客园 input text 的事件及方法
sql得到当前系统时间得 日期部分
代码泪 · 2011-06-23 · via 博客园 - 代码泪

sql得到当前系统时间得 日期部分

CONVERT(varchar(10),getDate(),120)

求得到"昨天,今天"日期函数的SQL

所属分类:MS-SQL Server 基础类

----------------------------------------------------------------------

Convert(Datetime,GetDate(),2)

GetDate()得到今天日期2007-03-26 16:14:12.187

1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?

2.求以下日期SQL:

昨天 

明天

最近七天

随后七天

上周

本周

下周

上月

本月

下月

请高手帮忙。谢谢

----------------------------------------------------------------------

1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?

select convert(varchar(10),getdate(),120)

--------------------------------------------------------

--1.

Select Convert(Varchar(10), GetDate(), 120)

Select Convert(Varchar(10), GetDate(), 121)

--------------------------------------------------------

2.求以下日期SQL:

昨天 

select convert(varchar(10),getdate() - 1,120)

明天

select convert(varchar(10),getdate() + 1,120)

最近七天

select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120)

随后七天

select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段

--------------------------------------------------------

convert和dateadd函数结合使用就可以了。

--------------------------------------------------------

用datediff(day,时间列,getdate())

--------------------------------------------------------

上月

select * from tb where month(时间字段) = month(getdate()) - 1

本月

select * from tb where month(时间字段) = month(getdate())

下月

select * from tb where month(时间字段) = month(getdate()) + 1

--------------------------------------------------------

--2

--如果是在表中查詢

--昨天 

Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1

--明天

Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = 1

--最近七天

Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7

--随后七天

Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) <= 7

--上周

Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 1

--本周

Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 0

--下周

Select * From TableName Where DateDiff(wk, GetDate(), DateTimCol ) = 1

--上月

Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 1

--本月

Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 0

--下月

Select * From TableName Where DateDiff(mm, GetDate(), DateTimCol ) = 1

--------------------------------------------------------

本周

select * from tb where datediff(week , 时间字段 ,getdate()) = 0

上周

select * from tb where datediff(week , 时间字段 ,getdate()) = 1

下周

select * from tb where datediff(week , 时间字段 ,getdate()) = -1

--------------------------------------------------------

1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?

select convert(varchar(10),getdate(),120)

2.求以下日期SQL:

昨天 

select convert(varchar(10),getdate() - 1,120)

明天

select convert(varchar(10),getdate() + 1,120)

最近七天

select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120)

随后七天

select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段

上月

select * from tb where month(时间字段) = month(getdate()) - 1

本月

select * from tb where month(时间字段) = month(getdate())

下月

select * from tb where month(时间字段) = month(getdate()) + 1

本周

select * from tb where datediff(week , 时间字段 ,getdate()) = 0

上周

select * from tb where datediff(week , 时间字段 ,getdate()) = 1

下周

select * from tb where datediff(week , 时间字段 ,getdate()) = -1

--------------------------------------------------------

昨天:dateadd(day,-1,getdate())

明天:dateadd(day,1,getdate())

上月:month(dateadd(month, -1, getdate()))

本月:month(getdate())

下月:month(dateadd(month, 1, getdate()))