Howto convert datetime to date with an ODBC function?

*爱你&永不变心* 提交于 2019-12-10 17:57:31

问题


Question:

I need to get the DATE ONLY (= date WITHOUT time) with an ODBC escape sequence.

However

SELECT 
     {fn CONVERT(SomeTable.Col_With_DateTime_Value, SQL_DATE)}
FROM SomeTable

does return the column as datetime value (date WITH time).

Is there any ODBC function I can use to get the date value only ?

Note:
This is not a duplicate question.
I know one can use the non-ODBC convert function, like

CONVERT(char(8),  getdate(), 112) AS v112_ISO  
CONVERT(char(10), getdate(), 104) AS v104_XML  

but I really need an ODBC function for compatibility reasons.


回答1:


You could use this:

select {fn convert({fn timestampdiff(SQL_TSI_DAY, 0, DatetimeCol)}, SQL_DATE)}
from SomeTable

Works in the environments I have available to test on (SQL Server 2000-2012).

Update:

Here is another way that I believe could be faster than using convert.

select {fn timestampadd(SQL_TSI_DAY, {fn timestampdiff(SQL_TSI_DAY, 0, DatetimeCol)}, 0)} 
from SomeTable



回答2:


select cast(day(getdate()) as varchar(2))+'/'+cast(month(getdate()) as varchar(2))+'/'+cast(year(getdate()) as varchar(4))

or

SELECT CONVERT(VARCHAR(10),GETDATE(),111)


来源:https://stackoverflow.com/questions/9922538/howto-convert-datetime-to-date-with-an-odbc-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!