select records by Date in SQL Server

本文介绍如何使用SQL语句筛选指定日期范围内的记录,并提供了利用COleDateTime类生成不同格式时间字符串的方法。此外,还详细解释了SQL Server中CONVERT函数的应用,包括其参数说明及日期格式转换的样式。

1, how to select

assume the table CAS.dbo._Test have a field AuthTime with the type datetime, We may use following SQL to select all the records in '2009-04-06'.

 

SELECT *

FROM CAS.dbo._Test

WHERE (AuthTime > CONVERT(DATETIME, '2009-04-06', 102)) AND

      (AuthTime < CONVERT(DATETIME, '2009-04-07', 102))

Or simply

SELECT *

FROM CAS.dbo._Test

WHERE (AuthTime > '2009-04-06') AND (AuthTime < '2009-04-07')

 

Here the string '2009-04-06' actually stands for '2009-04-06 AM 00:00:00'

Another example:

select CONVERT(VARCHAR(8),GETDATE(),112)

20090209

(1 row(s) affected)

 

2, some tips on getting time string

With the class COleDateTime you can convert the system time into a string with any form you like. The COleDateTime object can be initialed by time_t, SYSTEMTIME, or even:

COleDateTime time = COleDateTime(2001,6,15, 10, 8, 30);

If need the next day, just add 1 to the object. Call Format we can get the string form of time. Here is a example:

 

then the string cstrTimeFrom, cstrTimeTo are filled with '2009-04-06' and '2009-04-07'. The standard format: Format(_T("%Y-%m-%d %H:%M:%S"));

you may also get the right time string by MFC class CTime.

 

 

And there is some notes about the function CONVERT from MSDN:

Using CONVERT:

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Arguments

expression

Is any valid Microsoft® SQL Server™ expression. For more information, see Expressions.

data_type

Is the target system-supplied data type, including bigint and sql_variant. User-defined data types cannot be used. For more information about available data types, see Data Types.

length

Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types.

style

Is the style of date format used to convert datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types), or the string format when converting float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types).

SQL Server supports the date format in Arabic style, using Kuwaiti algorithm.

In the table, the two columns on the left represent the style values for datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).

 

Without century (yy)

With century (yyyy)


Standard


Input/Output**

-

0 or 100 (*)

Default

mon dd yyyy hh:miAM (or PM)

1

101

USA

mm/dd/yy

2

102

ANSI

yy.mm.dd

3

103

British/French

dd/mm/yy

4

104

German

dd.mm.yy

5

105

Italian

dd-mm-yy

6

106

-

dd mon yy

7

107

-

Mon dd, yy

8

108

-

hh:mm:ss

-

9 or 109 (*)

Default + milliseconds

mon dd yyyy hh:mi:ss:mmmAM (or PM)

10

110

USA

mm-dd-yy

11

111

JAPAN

yy/mm/dd

12

112

ISO

yymmdd

-

13 or 113 (*)

Europe default + milliseconds

dd mon yyyy hh:mm:ss:mmm(24h)

14

114

-

hh:mi:ss:mmm(24h)

-

20 or 120 (*)

ODBC canonical

yyyy-mm-dd hh:mi:ss(24h)

-

21 or 121 (*)

ODBC canonical (with milliseconds)

yyyy-mm-dd hh:mi:ss.mmm(24h)

-

126(***)

ISO8601

yyyy-mm-dd Thh:mm:ss.mmm(no spaces)

-

130*

Hijri****

dd mon yyyy hh:mi:ss:mmmAM

-

131*

Hijri****

dd/mm/yy hh:mi:ss:mmmAM

 

* The default values (style 0 or 100, 9 or 109, 13 or 113, 20 or 120, and 21 or 121) always return the century (yyyy).
** Input when converting to datetime; output when converting to character data.
*** Designed for XML use. For conversion from datetime or smalldatetime to character data, the output format is as described in the table. For conversion from float, money, or smallmoney to character data, the output is equivalent to style 2. For conversion from real to character data, the output is equivalent to style 1.

Source:

http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx

 

 

一、 内容概要 本资源提供了一个完整的“金属板材压弯成型”非线性仿真案例,基于ABAQUS/Explicit或Standard求解器完成。案例精确模拟了模具(凸模、凹模)与金属板材之间的接触、压合过程,直至板材发生塑性弯曲成型。 模型特点:包含完整的模具-工件装配体,定义了刚体约束、通用接触(或面面接触)及摩擦系数。 材料定义:金属板材采用弹塑性材料模型,定义了完整的屈服强度、塑性应变等真实应力-应变数据。 关键结果:提供了成型过程中的板材应力(Mises应力)、塑性应变(PE)、厚度变化​ 云图,以及模具受力(接触力)曲线,完整再现了压弯工艺的力学状态。 二、 适用人群 CAE工程师/工艺工程师:从事钣金冲压、模具设计、金属成型工艺分析与优化的专业人员。 高校师生:学习ABAQUS非线性分析、金属塑性成形理论,或从事相关课题研究的硕士/博士生。 结构设计工程师:需要评估钣金件可制造性(DFM)或预测成型回弹的设计人员。 三、 使用场景及目标 学习目标: 掌握在ABAQUS中设置金属塑性成形仿真的全流程,包括材料定义、复杂接触设置、边界条件与载荷步。 学习如何调试和分析大变形、非线性接触问题的收敛性技巧。 理解如何通过仿真预测成型缺陷(如减薄、破裂、回弹),并与理论或实验进行对比验证。 应用价值:本案例的建模方法与分析思路可直接应用于汽车覆盖件、电器外壳、结构件等钣金产品的冲压工艺开发与模具设计优化,减少试模成本。 四、 其他说明 资源包内包含参数化的INP文件、CAE模型文件、材料数据参考及一份简要的操作要点说明文档。INP文件便于用户直接修改关键参数(如压边力、摩擦系数、行程)进行自主研究。 建议使用ABAQUS 2022或更高版本打开。显式动力学分析(如用Explicit)对计算资源有一定要求。 本案例为教学与工程参考目的提供,用户可基于此框架进行拓展,应用于V型弯曲
SQL Server 中,创建和维护基于日期列(如 `DATE` 或 `DATETIME`)的分区表,并按年进行分区,是一种常见的优化手段,用于管理大规模数据,提高查询性能。 以下是创建和维护一个基于日期列按年进行分区的完整示例。 --- ## ✅ 示例场景: - 表名:`SalesData` - 分区列:`SaleDate` (DATE 类型) - 分区方式:每年一个分区 - 文件组:使用默认文件组 `PRIMARY` --- ## ✅ 步骤 1:创建文件组(可选) 为了更好的管理,我们可以为每个年份分区分配不同的文件组。这里为了简化,我们使用默认的 `PRIMARY` 文件组。 ```sql -- 可选:创建多个文件组(适合生产环境) -- ALTER DATABASE YourDatabase ADD FILEGROUP FG_2023; -- ALTER DATABASE YourDatabase ADD FILEGROUP FG_2024; -- ... ``` --- ## ✅ 步骤 2:创建分区函数 定义一个范围分区函数,根据 `DATE` 类型值进行分区,按年划分。 ```sql CREATE PARTITION FUNCTION pf_SalesByYear (DATE) AS RANGE RIGHT FOR VALUES ( '2020-12-31', '2021-12-31', '2022-12-31', '2023-12-31', '2024-12-31' ); ``` ### 解释: - `RANGE RIGHT`:表示边界值属于**右边**的分区。 - 例如 `'2020-12-31'` 属于第二个分区。 - 分区范围如下: - 分区 1: < '2020-12-31' (即 2020 年之前) - 分区 2: >= '2020-12-31' 且 < '2021-12-31' (即 2021 年) - 分区 3: >= '2021-12-31' 且 < '2022-12-31' (即 2022 年) - 以此类推... --- ## ✅ 步骤 3:创建分区方案 将分区函数映射到文件组。这里我们统一使用 `PRIMARY` 文件组。 ```sql CREATE PARTITION SCHEME ps_SalesByYear AS PARTITION pf_SalesByYear ALL TO ([PRIMARY]); ``` --- ## ✅ 步骤 4:创建分区表 使用分区方案创建表。 ```sql CREATE TABLE SalesData ( SaleID INT IDENTITY(1,1) PRIMARY KEY, ProductName NVARCHAR(100), SaleDate DATE NOT NULL, Amount DECIMAL(10,2) ) ON ps_SalesByYear (SaleDate); ``` --- ## ✅ 步骤 5:插入数据并查看分区 ```sql INSERT INTO SalesData (ProductName, SaleDate, Amount) VALUES ('Product A', '2021-05-15', 100.00), ('Product B', '2022-03-20', 200.00), ('Product C', '2023-08-10', 150.00); ``` --- ## ✅ 步骤 6:查看数据分区情况 ```sql SELECT $PARTITION.pf_SalesByYear(SaleDate) AS PartitionNumber, COUNT(*) AS Records FROM SalesData GROUP BY $PARTITION.pf_SalesByYear(SaleDate); ``` --- ## ✅ 步骤 7:维护分区(添加新分区) 当新一年的数据到来时(如 2025),需要扩展分区函数: ```sql ALTER PARTITION SCHEME ps_SalesByYear NEXT USED [PRIMARY]; ALTER PARTITION FUNCTION pf_SalesByYear() SPLIT RANGE ('2025-12-31'); ``` --- ## ✅ 步骤 8:合并或删除旧分区(可选) 例如,合并 2020 年之前的分区: ```sql ALTER PARTITION FUNCTION pf_SalesByYear() MERGE RANGE ('2020-12-31'); ``` --- ## ✅ 总结 以上步骤展示了如何在 SQL Server 中: - 创建基于日期列的分区函数 - 创建分区方案 - 创建分区表 - 插入数据并查看分区 - 动态维护分区(添加、合并) --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值