I need some help in understanding the following piece of code that I need to translate to SQL Server.
where
srch_req_dttm > ( Date - '+CAST(#Intval AS VARCHAR(10))+ ')
and srch_req_dttm < date
What does the "Date" part in above signify? Is it an equivalent of GETDATE() function in SQL Server?
DATE in Teradata will get the current date. To do the same in Sql Server you will need to use GETDATE()... but GETDATE() also returns the time, so it's not an exact match for the DATE function in Teradata.
You can use DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) in SQL Server, however, to get just the date back. It's pretty verbose, but I believe it's the closest match.
Related
Need to write select query that returns data that is older than a month and I need to use EPOCH.
select *
from TABLE
where DATE < (NOW() - 604800) <- does not work.
I don't understand why SQL Server would not have something this basic.
Actually, it's easier in SQL Server: you can achieve what you want to do with the following query;
select * from Table where Date < DATEADD(month, -1, GETDATE())
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'))
if any date time value provided to sql server can i get it's midnight value with some function in sql server.. for example if i provide 2013/07/03 01:34AM , i want to get it to 2013/07/03 12:00 AM.Is there a way to do it?
SQL Server 2008+
SELECT CAST(CAST('2013/07/03 01:34AM' AS date) AS datetime)
For older versions, see this Best approach to remove time part of datetime in SQL Server Never use anything that requires float or int or varchar conversions
This should give you what you need:
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, InputDateField), 0)
Should be slightly quicker than cast:
Most efficient way in SQL Server to get date from date+time?
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
I'm currently using
Convert(varchar, Getdate(), 101)
to insert only date part of system date into one of my sql server database tables.
my question is: is it the right way to do that or is there any other better method to do it?
I don't understand why you're converting the GETDATE() output (which is DATETIME already) to a VARCHAR and then SQL Server would convert it back to DATETIME upon inserting it again.
Just use:
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(GETDATE())
If you're doing that conversion to get rid of the time portion of the DATETIME, you should better:
use the DATE datatype (available in SQL Server 2008 and newer) to store only the DATE (no time)
if you're using SQL Server 2005 or earlier, use this conversion instead - should be much more efficient than two conversions!
INSERT INTO dbo.YourTable(SomeDateTimeColumn)
VALUES(DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
Update: did some performance testing, and in this particular case, it seems the amount of work that SQL Server needs to do is really the same - regardless of whether you're using the convert to varchar stripping the time and back to datetime approach that you already have, or whether you're using my get the number of days since date 0 approach. Doesn't seem to make a difference in the end.
The BEST solution however would still be: if you only need the date anyway - use a column of type DATE (in SQL Server 2008 and newer) and save yourself any conversions or manipulations of the GETDATE() output altogether.