问题
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