sql日期差异(以年为单位)-varchar列可能为空或null
我需要创建一个周年报告。这是原始代码sql日期差异(以年为单位)-varchar列可能为空或null,sql,sql-server,sql-server-2005,cfml,Sql,Sql Server,Sql Server 2005,Cfml,我需要创建一个周年报告。这是原始代码 SELECT cs.id AS csid , csi.Search_Number AS searchID , csi.date_of_placement AS datePlaced , ui.firstname , ui.lastname , cp.name AS companyname , cp.address_line_1 , cp.addr
SELECT cs.id AS csid ,
csi.Search_Number AS searchID ,
csi.date_of_placement AS datePlaced ,
ui.firstname ,
ui.lastname ,
cp.name AS companyname ,
cp.address_line_1 ,
cp.address_line_2 ,
cp.city ,
cp.state ,
cp.zip ,
cp2.name AS currentcompanyname ,
cp2.address_line_1 AS current_address_line_1 ,
cp2.address_line_2 AS current_address_line_2 ,
cp2.city AS current_city ,
cp2.state AS current_state ,
cp2.zip AS current_zip
FROM client_searches_individuals AS csi
LEFT JOIN users_info AS ui ON ui.id = csi.individual_number
LEFT JOIN client_searches AS cs ON cs.id = csi.search_number
LEFT JOIN companies AS Cp ON cp.id = cs.company_number
LEFT JOIN companies AS cp2 ON cp2.id = ui.current_company_number
WHERE 0 = 0
<!--- This is for the varchar date field, take away anything with the year selected --->
and csi.date_of_placement not like '#dateformat(attributes.startdate,'yyyy')#%'
<cfif attributes.startdate neq "">
and right(csi.date_of_placement,5) >= '#dateformat(attributes.startdate,'mm/dd')#'
</cfif>
<cfif attributes.enddate neq "">
and right(csi.date_of_placement,5) <= '#dateformat(attributes.enddate,'mm/dd')#'
</cfif>
AND ISNULL(cs.id, 0) > 0
ORDER BY date_of_placement
我得到
味精242,第16级,第3状态,第2行
将char数据类型转换为datetime数据类型导致datetime值超出范围
SQL Server 2005
CF 9
我做了一个查询以更正放置日期。然后是第二个查询或第一个查询,以选择一年或更久的任何内容
SELECT *
FROM placedIndGoodDates
WHERE 0 = 0
AND DATEDIFF(year, datePlaced, '#asofdate#') >= 1
ORDER BY date_of_placement
查询语法错误
遇到日期不同的年份。条件表达式不正确,应为[like | null | between | in | comparison]条件之一
我遗漏了一些基本信息。考虑执行子选择,将放置日期设置为DATETIME,并确定放置日期为空或为空的记录的处理方法 您可以使用WHERE子句排除这些行,或者根据需要在将来为它们指定任意日期 一旦完成,您就可以使用日期范围比较而不产生问题,而不是手动比较年份等并遇到边界问题 更新: 根据额外信息,在将这些值输入DATEDIFF之前,使用WHERE子句删除无效条目,例如
SELECT
CSI.OriginalDate,
DATEDIFF(year, CSI.OriginalDate, GETDATE()) AS YearBoundariesCrossed,
DATEDIFF(month, CSI.OriginalDate, GETDATE()) / 12 AS KindaFullYearsDifference
FROM (
-- sub-select to remove invalid dates and convert to date
SELECT
CAST(date_of_placement AS datetime) AS OriginalDate,
-- specify just the fields you need, or use the lazy *
*
FROM client_searches_individuals
-- use whatever check(s) you need here, I like to check for LEN() > 0
-- NULL entries fail this test too, which is good
WHERE LEN(date_of_placement) > 0
) CSI
-- could join "CSI" to your other tables here, not doing that for this sample
-- normal date comparison OK here
WHERE CSI.OriginalDate < GETDATE()
对于额外的点,我已经包括了DATEDIFFyear之间的差异。。和DATEDIFFmonth,…/12如果您希望跨越至少12个月的边界,则后者更好
注:DATEDIFF只计算这一点,当使用“年”或“月”或“日”等时,跨越2013-12-31与2014-01-01的界限=1。我不知道为什么,但使用dateadd代替DATEDIFF是有效的。你到底是如何使用dateadd的?因为它不能替代DateDiff.和csi.date_of_placement,它可以根据当前日期返回至少一年前的所有记录。但是,请注意,它依赖于隐式转换,这是不好的,原因有两个:一是您无法控制字符串的转换方式。结果可能并不总是您所期望的,并且可能根据db日期设置和表b中的实际值而有所不同。db将无法利用这些列上的索引。如果可能的话,最好将列数据类型更改为datetime。我希望重新设计数据库。但那是一种方式,一种方式,一种方式超出了范围。为什么日期存储为varchar?日期值应存储为日期,而不是字符串。这是一个遗留应用程序吗?
SELECT
CSI.OriginalDate,
DATEDIFF(year, CSI.OriginalDate, GETDATE()) AS YearBoundariesCrossed,
DATEDIFF(month, CSI.OriginalDate, GETDATE()) / 12 AS KindaFullYearsDifference
FROM (
-- sub-select to remove invalid dates and convert to date
SELECT
CAST(date_of_placement AS datetime) AS OriginalDate,
-- specify just the fields you need, or use the lazy *
*
FROM client_searches_individuals
-- use whatever check(s) you need here, I like to check for LEN() > 0
-- NULL entries fail this test too, which is good
WHERE LEN(date_of_placement) > 0
) CSI
-- could join "CSI" to your other tables here, not doing that for this sample
-- normal date comparison OK here
WHERE CSI.OriginalDate < GETDATE()