Need only Date from DateTime - sql-server

I have a variable of DateTime type in SQL.
Just need to have Date part of it.
please Help?

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
The result is: “2009-07-14 00:00:00.000”
Edit: guess the next variant is more common:
SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
because of the day pattern can be easily changed to week or month pattern. It is very useful when the GROUP BY clause should group by week of month (reports).

This has been asked and answered before on Stack Overflow. In fact, it's been asked over and over:
Most efficient way in MS SQL to get date from date+time?
Best way to check for current date in where clause of sql query.
SQL Drop Time in DateTime
MS SQL Date Only Without Time
How to return the date part only from a SQL Server datetime datatype

Found this using Google
SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE())))

If you just need a varchar representation of the date, you can use the convert function, e.g.
select convert(varchar, getDate(), 102) /* 2009.07.14 */
If you need a datetime (midnight on the given date), you can just convert it back.
select convert(datetime, convert(varchar, getDate(), 102), 102)

-- Sneaky CAST/DATEDIFF trick strips off the time to get just the day (midnight)!
CAST(DATEDIFF(d,0,DateField) AS DATETIME) AS DayField

SQL Server 2008 has a date datatype that stores just the date, if you are inthis version, perhaps this would be a better datat type for you to use. Be warned though, Date doesn't work exactly like datetime for data manipulation.

SELECT DATEADD(day, DATEDIFF(day, '19900101', CURRENT_TIMESTAMP), '19900101')
A very useful article:
"The purpose of this article is to explain how the datetime types work in SQL Server, including common pitfalls and general recommendations."The ultimate guide to the datetime datatypes
Note that converting to varchar and back (convert(datetime, convert(varchar, getDate(), 102), 102)) is much slower.

If you want the format 'MM/DD/YY', use "CONVERT(varchar, #datetimevalue, 1) to display just the date. If you need it in datetime format, use "CONVERT(datetime, CONVERT(varchar, #datetimevalue, 1))".
I created an entry in my SQL blog about how to retrieve and display all possible formats of the CONVERT(varchar, ..) function:
http://jessesql.blogspot.com/2009/04/converting-datetime-values-to-varchar.html

A tip:
If you find yourself doing this often, you can create a scalar User Defined Function containing the time-stripping logic of your choice.
Be warned: SQL Server 2000 has some painful bugs involving UDF's in ON clauses.

datepart(day, datetimevalue)

Related

slice data between the range of dates SQL Server

I am having a miserable time in achieving a simple task. I want to slice data for every 6-months every month. I am not getting any output. My SQL skills are very bad. I searched for the solution a lot and they gave me some idea but I am not able to any output.
Below is my attempt:
SELECT TOP 10000 CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) BETWEEN CONVERT(VARCHAR(10), GETDATE(), 110) AND CONVERT(VARCHAR(10), DATEADD(month, -6, GETDATE()), 110)
The table definitely has the data for the time period I am trying to query. So I am not sure why this is not giving me any output. I will really appreciate your help. Thank you.
I believe your BETWEEN values are backwards.
EDIT: Your CONVERT statements in the WHERE clause are also different formats, so when you compare your varchars, the comparison fails. You should instead use DATETIME.
Try this:
SELECT TOP 10000
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CAST(DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')) AS DATETIME) BETWEEN DATEADD(month, -6, GETDATE()) AND GETDATE()

How to default the time for the date with SQL Server

Does anyone know how can I default the time for the date with SQL Server?
Example:
When I use getdate() and it will return me the current date and time. How can I get the current date but default the time portion to '10:00:00.000' ?
This works for SQL Server (SQLFiddle):
SELECT DATEADD(hour, 10, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
Explanation: GETDATE() gives current date and time. Casting to DATE makes it date only (midnight time). Casting it again to DATETIME makes it compatible with DATEADD, which finally adds 10 hours to it.
Use the below if you are using sql server.
select cast(cast(getdate()AS INT)+0.41667 as datetime)
Note time is scaled on a 0.0 to 0.9999, and the no. of hours are equally distributed. e.g. 0.5 will give 12a.m.
This is from DB2. But same concept should work very DB. Just convert that date in to Timestamp
SELECT TIMESTAMP(CURRENT_DATE, TIME('10:00:00')) AS MY_DATE FROM SYSIBM.SYSDUMMY1
in sql server 2008 and above:
select convert(datetime,convert(varchar(10),convert(date,(GETDATE())))+' 00:00:00')
select convert(datetime,convert(varchar,convert(date,getdate())) + ' 10:00:00.000')
SQL FIDDLE
Again, in Ms SQL Server, You can also use
Select DateAdd(day, datediff(day, getdate()), 0) + 10/24.0

T-SQL version of MySQLs CURTIME()

I am trying to get teh current time as a unix timestamp from my MSSQL database.
In Mysql I could say something like:
SELECT id,
caregiver_id,
client_id,
week_no,
CURTIME() as Synch_Time
FROM dbo.Visits
But THERE is no CURTIME() function in T-SQL
Does anyone know of a solution?
Thanks
Kevin
You can use getdate() and do some calculations described here.
http://mysql.databases.aspfaq.com/how-do-i-convert-a-sql-server-datetime-value-to-a-unix-timestamp.html
getdate() is MSSQL specific, but current_timestamp() is more standard compliant. See similar question: Retrieving date in sql server, CURRENT_TIMESTAMP vs GetDate()
In SQL2012, use can convert to specific date and time types, e.g.
SELECT
Time = CONVERT(TIME, GETDATE()),
Date = CONVERT(DATE, GETDATE())

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

SQL Server 2008 script - how to to acquire current date from system and store it into a date column

Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?
On top of that, how do I add years to the current date?
I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?
Because sometimes I'd like to manually insert values into a column with that type in mind.
Any help would greatly be appreciated.
Thanks.
To get date only (SQL Server 2008 only) CAST to date type
SELECT CAST(GETDATE() AS date)
To add years, use DATEADD
SELECT DATEADD(year, 2, CAST(GETDATE() AS date))
Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server
To add a year to the current date, look at the dateadd() function.
To just get the date from sql w/o the time, you can do this:
DECLARE #Date DATETIME
SELECT #Date = CONVERT(VARCHAR, GETDATE(), 101)
SELECT #Date
Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.
Bender

Resources