DateAdd/DateDiff functions in H2 and SQL Server - sql-server

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'))

Related

Convert IIF() and datepart Access statement to SQL Server

I have a query that pulls some information a given week and it should be on a week day hence the datepart portion. can't get this command to pull into SQL correctly.
IIf(DatePart('w',Date())=2,Date()-3,Date()-1) And Date()))
The above is from the SQL view of access.
dateadd(day,
case datepart(weekday, getdate())
when 2 then -3 else -1 end, /* assumes datefirst is 7 */
cast(getdate() as date)
)

TeraData Date function to SQL Server equivalent

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.

What is the SSRS expression for end day of week?

I have the following MSSQL query to return the ending day of the week in MSSQL:
SELECT DateAdd(Day, 0 - DatePart(Weekday, GetDate()), GetDate());
I played around with the =DateAdd function, but it keeps throwing me an error for the Day parameter. Also, when I used DateInterval.Day... I get the same error.
However, when I try placing that query into an SSRS expression, it throws me an error. Does anyone know the direct conversion for that query above in SSRS?
SSRS Uses a dialect of Visual Basic, its Date functions are different from TSQL, you have to use
"d" instead of DAY for day interval
"w" instead of WEEKDAY for weekday
Now() instead of GetDate() for current date.
Try
=DateAdd("d", 0 - DatePart("w", Now()), Now())

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

sql select datetime stamp as m/d/y only. (without h m s)

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)

Resources