I had used this as Where Condition in Oracle
(ProjectDate between trunc(sysdate-1)+15/24 and trunc(sysdate)+8/24)
I was trying to convert view to SQL Server and I used to try
ProjectDate between (GetDate()-1)+15/24 and (GetDate())+8/24
I am not sure if using right function or not?
You need to use following expression:
ProjectDate between
DATEADD(HOUR,15,CONVERT(DATETIME, CONVERT(DATE, GETDATE())) - 1)
and DATEADD(HOUR,8,CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
DATEADD: The DATEADD() function adds a time/date interval to date and then returns the date.
CONVERT(DATETIME, CONVERT(DATE, GETDATE())) returns the same as TRUNC(SYSDATE) in oracle
Related
I am writing an munit test case and mocking my real SQL Server database tables with H2 in-memory instance.
My query has date condition to return records from previous month, so I was looking for something that works with SQL Server as well as H2. Finally I found below which syntactically works in both.
select
DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()) - 1, '1900-01-01');
In SQL Server, it properly gives me start date of previous month but in H2, it is giving me start date of current month. If change -1 to 2 then H2 works but SQL Server returns the wrong date.
Can someone please help with how I can get correct syntax?
Same problem with last day of previous month -
select
DATEADD(D, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', GETDATE()), '1900-01-01'))
TRUNC(LD.FECHA_CREA)
I need convert it to SQL Server
You can use following condition in your query :
CONVERT(DATETIME, CONVERT(DATE, LD.FECHA_CREA)) = CONVERT(DATETIME, CONVERT(DATE, GETDATE()))
You could use this equivalent in SQL Server:
CONVERT(DATETIME, CONVERT(DATE, LD.FECHA_CREA));
Check: http://www.sqlines.com/oracle-to-sql-server/trunc_datetime
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
When I select two rows with a DATETEIME stamp, I only want the m/d/y data and nothing after that.
It has to changed during the select (not afterwards).
To remove the time you just need to do the following Assuming SQL Server
Pre SQL 2008
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) yourdate
FROM yourtable
SQL 2008
select CAST(getdate() as date) yourdate
FROM yourtable
See Most efficient way in SQL Server to get date from date+time?
or
Best approach to remove time part of datetime in SQL Server
Is this for export? If you only want the text you can use a variety of coversion formats available on MSDN.
select convert(varchar, getdate(), 101)
-- output: 07/05/2011
Otherwise, if you're using sql 2008, you can just cast the datetime to date:
select cast(getdate() as date)
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)