Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 396 Vote(s) - 3.38 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trunc(sysdate) in SQL Server

#1
What is the equivalent of:

TRUNC(SYSDATE)

...in SQL Server 2005?
Reply

#2
Recommended:

DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

This is another alternative, but it's risky because of casting to a FLOAT. It's also been demonstrated to not scale performance as well as the DATEADD/DATEDIFF approach.

CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
Reply

#3
Another option is to use [CONVERT](

[To see links please register here]

) (MSSQL 2008 and later) and either use an appropriate style or use a style that you can then SUBSTRING. I have no idea about the performance compared to the dateadd/datediff solution though.

e.g.

`SELECT SUBSTRING(CONVERT(nvarchar(30), GETDATE(), 120), 1, 16)`

Returns:

`2012-01-03 15:30`

Example using group that lists rows created per minute (presupposes a 'created' datetime column):

SELECT SUBSTRING(CONVERT(nvarchar(30), created, 120), 1, 16) as [minute]
, COUNT(1) as [per min]
FROM foo
GROUP BY SUBSTRING(CONVERT(nvarchar(30), created, 120), 1, 16)

Reply

#4
As of SQL Server 2022 CTP 2.1, DATETRUNC() is now supported which should line up well with the TRUNC() capability in Oracle. Documentation can be found [here][1].


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through