How to select date without time in SQL - sql

When I select date in SQL it is returned as 2011-02-25 21:17:33.933. But I need only the Date part, that is 2011-02-25. How can I do this?

For SQL Server 2008:
Convert(date, getdate())
Please refer to https://learn.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql

I guess he wants a string.
select convert(varchar(10), '2011-02-25 21:17:33.933', 120)
120 here tells the convert function that we pass the input date in the following format: yyyy-mm-dd hh:mi:ss.

Using CAST(GETDATE() As Date) worked for me

The fastest is datediff, e.g.
select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
from tbl
But if you only need to use the value, then you can skip the dateadd, e.g.
select ...
WHERE somedate <= datediff(d, 0, getdate())
where the expression datediff(d, 0, getdate()) is sufficient to return today's date without time portion.

CAST(
FLOOR(
CAST( GETDATE() AS FLOAT )
)
AS DATETIME
)
http://www.bennadel.com/blog/122-Getting-Only-the-Date-Part-of-a-Date-Time-Stamp-in-SQL-Server.htm

For 2008 older version :
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

you can use like this
SELECT Convert(varchar(10), GETDATE(),120)

In case if you need the time to be zeros like 2018-01-17 00:00:00.000:
SELECT CONVERT(DATETIME, CONVERT(DATE, GETDATE()), 121)

I would use DATEFROMPARTS function. It is quite easy and you don't need casting. As an example this query :
Select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE())) as myNewDate
will return
2021-01-21
The good part you can also create you own date, for example you want first day of a month as a date, than you can just use like below:
Select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1) as myNewDate
The result will be:
2021-01-01

You can try this one too.
SELECT CONVERT(DATE, GETDATE(), 120)

Its too late but following worked for me well
declare #vCurrentDate date=getutcdate()
select #vCurrentDate
When data type is date, hours would be truncated

It's a bit late, but use the ODBC "curdate" function (angle brackes 'fn' is the ODBC function escape sequence).
SELECT {fn curdate()}
Output: 2013-02-01

Convert it back to datetime after converting to date in order to keep same datatime if needed
select Convert(datetime, Convert(date, getdate()) )

If you want to return a date type as just a date use
CONVERT(date, SYSDATETIME())
or
SELECT CONVERT(date,SYSDATETIME())
or
DECLARE #DateOnly Datetime
SET #DateOnly=CONVERT(date,SYSDATETIME())

Use is simple:
convert(date, Btch_Time)
Example below:
Table:
Efft_d Loan_I Loan_Purp_Type_C Orig_LTV Curr_LTV Schd_LTV Un_drwn_Bal_a Btch_Time Strm_I Btch_Ins_I
2014-05-31 200312500 HL03 NULL 1.0000 1.0000 1.0000 2014-06-17 11:10:57.330 1005 24851e0a-53983699-14b4-69109
Select * from helios.dbo.CBA_SRD_Loan where Loan_I in ('200312500') and convert(date, Btch_Time) = '2014-06-17'

select DATE(field) from table;
field value: 2020-12-15 12:19:00
select value: 2020-12-15

In PLSQL you can use
to_char(SYSDATE,'dd/mm/yyyy')

First Convert the date to float (which displays the numeric), then ROUND the numeric to 0 decimal points, then convert that to datetime.
convert(datetime,round(convert(float,orderdate,101),0) ,101)

Try this.
SELECT DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE()))

I would create a scalar function and use format () to set the datatype you want to see. It is must easy on the maintenance later.

Personal favorite:
select convert(datetime, convert(int, getdate()))

Related

How to get only date part while using dateadd() and getdate()

I want to display records of last 4 months from current date.
I don't want to consider time
How can I get just date part from the below query?
where OrderDate >= DATEADD(month, -4, GETDATE())
If you're using SQL Server 2008, try converting GETDATE() to a DATE directly.
WHERE OrderDate >= DATEADD(month, -4, CONVERT(date, GETDATE()))
http://sqlfiddle.com/#!3/df444/2
Why not use the simple DATEDIFF function
where DATEDIFF(MM, OrderDate, GETDATE()) < 4
If you can't use the DATE type, there's the old way: convert the DATETIME value to CHAR, trim the hour components and then convert it back to DATETIME, so the hour components will be zeroed:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112), 112)
-- -----------------------
-- 2014-02-25 00:00:00.000
The important thing is to use the function over the scalar parameter (and not on the column) to allow the usage of existing indexes.

How to get 00:00:00 in datetime, for First of Month?

I wrote a query to obtain First of month,
SELECT DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as First_Of_Month;
for which i do get the appropriate output, but my time stamp shows the current time.
Here's what I am doing in the query, hope i make sense.
using datepart i calculated the no. of days (int) between the 1st and today (27-1 =26)
Then using dateadd function, i added "-datepart" to get the first of the month.
This is just changing the date, what should i look at or read about in order to change the time. I am assuming that it would have something to do with 19000101
For SQL Server 2012 (thanks adrift and i-one)
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
SELECT DATEADD(DAY, 1, EOMONTH(#now, -1));
-- or
SELECT DATEFROMPARTS(YEAR(#now), MONTH(#now), 1);
For SQL Server 2008+
DECLARE #now DATETIME = CURRENT_TIMESTAMP;
-- This shorthand works:
SELECT CONVERT(DATE, #now+1-DAY(#now));
-- But I prefer to be more explicit, instead of relying on
-- shorthand date math (which doesn't work in all scenarios):
SELECT CONVERT(DATE, DATEADD(DAY, 1-DAY(#now), #now));
For SQL Server 2005
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0);
A caveat if you're using this SQL Server 2005 method: you need to be careful about using expressions involving DATEDIFF in queries. SQL Server can transpose the arguments and lead to horrible estimates - as seen here. It might actually be safer to take the slightly less efficient string approach:
SELECT CONVERT(DATETIME, CONVERT(CHAR(6), GETDATE(), 112) + '01');
SELECT convert(varchar(10),DATEADD(DAY, - (DATEPART(DAY,GETDATE())-1), GETDATE()),120)+' 00:00:00' as First_Of_Month;
Just the date
DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
So for a month it is:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS FirstDatetimeOfMonthmm,
I think the easiest way is to cast the result to date:
SELECT cast(DATEADD(DAY, -(DATEPART(DAY,GETDATE())-1), GETDATE()) as date) as First_Of_Month
One alternative:
SELECT cast(
cast(datepart(yyyy, getdate()) as varchar)
+ '-'
+ cast(datepart(mm, getdate()) as varchar) + '-01 00:00:00'
as datetime)
Build up the date from year/month components, then tack on the 1st and midnight.
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as First_Of_Month;
Try following code:
SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME) AS StartDateTime,
DATEADD(ms, -3, CAST(CONVERT(date, DATEADD (DAY,1,GETDATE())) AS varchar(10))) AS EndDateTime
Result:
StartDateTime
EndDateTime
2022-05-23 00:00:00.000
2022-05-23 23:59:59.997

Subtract one day from datetime

I have a query to fetch date diff between 2 datetime as :
SELECT DATEDIFF(DAY, #CreatedDate , GETDATE())
Ex :
SELECT DATEDIFF(DAY, '2013-03-13 00:00:00.000' , GETDATE())
I need to have a query work like this which will subtract a day from created day:
SELECT DATEDIFF(DAY, **#CreatedDate- 1** , GETDATE())
Try this
SELECT DATEDIFF(DAY, DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())
OR
SELECT DATEDIFF(DAY, DATEADD(day, -1, #CreatedDate), GETDATE())
I am not certain about what precisely you are trying to do, but I think this SQL function will help you:
SELECT DATEADD(day,-1,'2013-04-01 16:25:00.250')
The above will give you 2013-03-31 16:25:00.250.
It takes you back exactly one day and works on any standard date-time or date format.
Try running this command and see if it gives you what you are looking for:
SELECT DATEADD(day,-1,#CreatedDate)
To simply subtract one day from todays date:
Select DATEADD(day,-1,GETDATE())
(original post used -7 and was incorrect)
Apparently you can subtract the number of days you want from a datetime.
SELECT GETDATE() - 1
2016-12-25 15:24:50.403
This should work.
select DATEADD(day, -1, convert(date, GETDATE()))
SELECT DATEDIFF (
DAY,
DATEDIFF(DAY, #CreatedDate, -1),
GETDATE())
Try this, may this will help you
SELECT DATEDIFF(DAY, DATEADD(DAY,-1,'2013-03-13 00:00:00.000') , GETDATE())
To be honest I just use:
select convert(nvarchar(max), GETDATE(), 112)
which gives YYYYMMDD and minus one from it.
Or more correctly
select convert(nvarchar(max), GETDATE(), 112) - 1
for yesterdays date.
Replace Getdate() with your value OrderDate
select convert(nvarchar (max),OrderDate,112)-1 AS SubtractDate FROM Orders
should do it.
You can try this.
Timestamp=2008-11-11 13:23:44.657;
SELECT DATE_SUB(OrderDate,INTERVAL 1 DAY) AS SubtractDate FROM Orders
output :2008-11-10 13:23:44.657
I hope, it will help to solve your problem.

How do I concatenate numbers to create a custom date?

Here is what I have tried thus far:
select CAST(
DATEPART(month,getDate())+'-'+
DATEPART(day,getDate())+'-'+
2012
as datetime)
I end up with the date: 1905-08-02 00:00:00.0. I was expecting to get today's date. I have rearranged the order and it doesn't seem to change. Can anyone offer as to why it gives me this? For the record, I plan to use other values than 2012 for the year.
Thanks in advance.
CAST() each piece as a varchar first:
select
cast(
cast(DATEPART(month,getDate()) as varchar(2))+'-'+
cast(DATEPART(day,getDate()) as varchar(2))+'-'+
'2012' as datetime)
select CAST ('2012'+
CAST(DATEPART(month,getDate()) as char(2))+
CAST(DATEPART(day,getDate()) as char(2))
as datetime)
You have to concatenate strings. Your code is casting the number 2039 to date.
If the goal with this little exercise is to be able to change the year of a given date you can do like this instead.
declare #NewYear int = 2003
-- with time part
select dateadd(year, #NewYear - year(getdate()), getdate())
-- time part removed
select dateadd(year, #NewYear - year(getdate()), dateadd(day, 0, datediff(day, 0, getdate())))
This code will work, you need to make sure that you are concatenating same data types and use convert with specific DateTime Format:
SELECT CONVERT(DATETIME,
CAST(DATEPART(month,getDate()) AS NVARCHAR(50))
+'-'+CAST(DATEPART(day,getDate()) AS NVARCHAR(50))
+'-2012'
,121)

checking for smalldatetime column equal to GetDate() - ignoring time

I have a column of smalldatetime type, date
I'd like to run a query that only retrieves rows:
where date = convert(smalldatetime,GetDate())
However, this is never finding matches as it is also comparing the times.
Ie: 01-01-2010 12:23:00 != 01-01-2010 12:25:00
How can I find matches on only the date portion?
One way which will utilize the index
where date >= dateadd(dd, datediff(dd, 0, getdate()), 0)
and date < dateadd(dd, datediff(dd, 0, getdate()), +1)
See also: How Does Between Work With Dates In SQL Server?
Try:
where datediff(dd, yourdate, GetDate()) = 0
Convert your DateTime values to Date values. That will store only the date portion for you to compare.
A list of available Date and Time datatypes in MSSQL is here.
You could try:
Where CAST(FLOOR(CAST([Date] AS FLOAT)) AS DATETIME) = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
This would make both dates appear as:
2010-08-05 00:00:00.000
and thus only comparing the dates ignoring the times
found this:
CAST(CONVERT (CHAR(8), GETDATE(), 112) AS smalldatetime)
from here:
reference
this sql worked for me when I added a smalldatetime field to an existing table:
SELECT testing
FROM LTCCAssessment
WHERE (testing = CAST(CONVERT(CHAR(8), GETDATE(), 112) AS smalldatetime))
where cast(CONVERT(char(10),DATETIME_FIELD_1,101) as datetime)
= cast(CONVERT(char(10),DATETIME_FIELD_2,101) as datetime)