--转换时间格式 返回 yyyy-MM-dd格式 if object_id('Get_yyyy_MM_dd') is not null drop function Get_yyyy_MM_dd go create function Get_yyyy_MM_dd(@dt datetime) returns varchar(100) as begin DECLARE @now VARCHAR(10) set @now = CONVERT(varchar(100), @dt, 23) return @now end go --转换时间格式 返回 yyyy-MM格式 if object_id('Get_yyyy_MM') is not null drop function Get_yyyy_MM go create function Get_yyyy_MM(@dt datetime) returns varchar(7) as begin DECLARE @now VARCHAR(7) set @now = CONVERT(varchar(100), @dt, 23) return @now end go sql 语句 select * from tb_test where dbo.Get_yyyy_MM_dd(add_time) between '2012-01-21' and '2013-05-16' select order_date,* from dbo.tb_sales_orders where order_date<>'' and datalength(order_date)<19