Howto convert datetime to date with an ODBC function? - sql

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.

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

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)

Related

SQL table column need to change datetime format to date

I have a column in a table with this format '2017-05-09 14:52:32.000' I would appreciate a way to convert it to a date format like MM/DD/YYYY
I have tried with:
select DATEADD(column, DATEDIFF(column, 0, getdate()), 0)
FROM table
but I get this error:
Column is not a recognized datediff option.
I've also tried another way and failed.
Please advise.
I'm assuming from the error message you reported that you're on SQL Server.
In SQL Server, the DATEADD and DATEDIFF functions take a "date part" as the first argument: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND or MILLISECOND:
SELECT DATEADD(DAY, DATEDIFF(DAY, [COLUMN], GETDATE()), [COLUMN])
FROM [TABLE]
might be want what you want.
However,
SELECT CONVERT(DATE, [COLUMN]) AS [COLUMNDATE]
Might be even easier.

Remove nano seconds from "Time" SQL

I have the following code in SQL
select "Time"
from Order
which returns - 09:10:11.0000000
How do I format this to give me just the hh:mm:ss ?
You can try like this:
SELECT CONVERT(VARCHAR, [time], 20) as 'Time'
from Order
TIME_FORMAT(time, '%T') will help you.
SELECT TIME_FORMAT(time, '%T') AS newTime FROM Order
It depends on the SQL implementation, but for SQL Server you should probably use the CONVERT() function.
Alternatively you could perform a substring on what is currently returned, or even a combination of the two.
Cast the value to time(0). The value in parentheses specifies the fractional seconds precision.
select cast([Time] as time(0)) as [Time]
time (Transact-SQL)
You should convert the field with date/time Styles.
Styles 20,108 will do the job:
select CONVERT(VARCHAR, [time], 108) as 'Time' from Order
select CONVERT(VARCHAR, [time], 20) as 'Time' from Order

How to only check the time on datetime fields but ignore the date?

I have a column that stores data in datetime format. I want to check for all instances where the time part of this column is not equal to 00:00:00:000 - the date does not matter.
Basically, if time() was a function, something like this:
SELECT *
FROM progen.DY
WHERE TIME(DY_DATE) <> '00:00:00:000'
How do I go about doing this?
You only need a minor tweak on what you already have.
SELECT *
FROM progen.DY
WHERE TIME(DY_DATE) <> '00:00:00:000'
Use CONVERT to change your DATETIME to a TIME.
SELECT *
FROM progen.DY
WHERE CONVERT(TIME, DY_DATE) <> '00:00:00:000'
Another way is to convert it to different datatype, eg
SELECT *
FROM progen.DY
WHERE CAST(DY_DATE as float) - CAST(DY_DATE as int) > 0
SQLFiddle Demo
I do this all the time when trying to see if a table's column should be turned into a date instead of a datetime, which is really the answer.
select *
from progen.dy
where cast(dy_date as Date) <> dy_date
the cast removes the time and datetime has higher precedence, so when compared, if the are unequal then it has a time value. Same thing could be done with a cast to time, with a bit of different syntax.
Use DATEDIFF and DATEADD to instead get the date part of the datetime. Compare the column against the date only, and it will return those rows that have a non-zero time.
The way this works is that we first calculate the difference (in days) between the epoch and the value. We add that number to the epoch to create a new datetime. Since the result of DATEDIFF is an integer, any time component gets rounded off.
SELECT *
FROM Table
WHERE DateColumn <> DATEADD(d, DATEDIFF(d, 0, DateColumn), 0)
The time function could then be implemented by the following, not that I recommend it for this specific scenario:
SELECT DATEDIFF(minute, DATEADD(d, DATEDIFF(d, 0, DateColumn), 0), DateColumn) as MinutesIntoDay,
-- or, if you require higher precision
DATEDIFF(second, DATEADD(d, DATEDIFF(d, 0, DateColumn), 0), DateColumn) as MinutesIntoDay
FROM Table
Edit: As mentioned in other answers, you can cast to DATE to achieve the same effect as DATEADD(d, DATEDIFF(d, 0, DateColumn), 0), which cleans up nicely. However, DATE was only added in SQL Server 2008, whereas the formula has compatibility back to at least SQL 2000. So if you need the backwards compatibility or are dealing with SQL CE, casting to DATE is unavailable.
SELECT *
FROM progen.DY
WHERE CONVERT(TIME, DY_DATE - CONVERT(DATE, DY_DATE)) > '00:00'

Date without the time

I'm working with SQL Server 2005.
I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.
Thanks,
Barry
EDIT:
Would it be safe to get the datetime and split it via Python on the first space, or is this format locale dependant?
In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.
SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101')
FROM dbo.table;
EDIT
If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:
SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
FROM dbo.table;
In SQL Server 2008 you can use the newly added date type:
select convert(date, purchase_time) from TableName
Update:
In versions prior to SQL 2008, I used the following solution for this problem:
select convert(datetime, convert(int, convert(float, purchase_time)))
from TableName

How do I convert a datetime field to a formatted string in SQL Server 2005?

I want to SELECT a formatted date string from a datetime type in SQL Server 2005.
In the format "yyyy/mm/dd hh:mm:ss".
What is the best way to do using only a query?
Check out the CONVERT statement.
SELECT CONVERT(VARCHAR(20), getdate(), 120)
is closest to what you want. (Note the different separators (- instead of / ))
select convert(varchar, datetime_field, 120) from tablename;
will do almost what you want.
120 is the conversion "style", see here for more.