Using sql DATEADD function in java - sql

When I run queries using DATEADD it seems that the database does not recognize this function.
also when I just run select DATEADD(Month, -3, GETDATE()) I'm getting:
Error code -1, SQL state 42X01: Syntax error: Encountered "<EOF>" at line 1, column 36.
I added the JAR file from hsqldb-2.2.9 as you can see
What am I missing here?

Derby does not have a DATEADD function. You need to use the JDBC function timestampadd to achieve this:
select {fn TIMESTAMPADD(SQL_TSI_MONTH, -3, CURRENT_TIMESTAMP)}
from sysibm.sysdummy1
An alternative to using sysdummy1 is the ANSI standard values clause which works both in Derby and HSQLDB:
values ({fn TIMESTAMPADD(SQL_TSI_MONTH, -3, CURRENT_TIMESTAMP)})

ADD OR SUBTRACT ONE DAY IN DERBY
select rundate, {fn TIMESTAMPADD(SQL_TSI_DAY, -1, RUNDATE)} from tst.YOUTH

For hsqldb:
DATEADD ( , , )
DATEADD ( 'month', 3, DATE '2008-11-22' )
Try it
select DATEADD(Month, -3, '2008-11-22')
For Derby:
Derby supports the JDBC escape function TIMESTAMPADD
TIMESTAMPDIFF( interval, timestampExpression1, timestampExpression2 )
values {fn timestampadd(SQL_TSI_DAY, 1, timestamp('2010-12-31 23:59:59'))};

Related

Eomonth function on SQL Server 2014

I am getting an error on this function, even it is defined. What could cause this problem?
EOMONTH() was introduced in SQL Server 2012.
I suspect the Compatibilty level of your database is set below that (or you are in fact not on SQL Server 2014).
ALTER DATABASE database_name
SET COMPATIBILITY_LEVEL = 120 -- 120 == SQL Server 2014
To determine your SQL Server version run:
SELECT ##VERSION
An alternative way of calculating EOMONTH is:
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, #Date) + 1, 0))
EOMONTH() returns a date type
REPLACE ( string_expression , string_pattern , string_replacement )
REPLACE ( EOMONTH( cast(#variable as date) ) '-','.')
You are attempting an implicit conversion from date to string, and it seems you are assuming that a date data type actually contains a dash, which isn't true. Just use format to the desired output. No need for replace and use a set of DATEADD() functions to substitute for EOMONTH()
select format(dateadd(day,-1,dateadd(month, datediff(month,0, getDate()), 0)),'yyyy.MM.dd')

How to modify year with a Cast Statement in Sql

I would like to get some help with a CAST SQL Statement
From the below table sample, I would like to modify only the year from 2008 to 2016. Please keep in mind that I need this done with CAST, and not with DATEADD.
DATEADD(YEAR, 8, DATE) would be the solution but if that is not an option please utilize one of the following options.
I have left the DATEADD options in there in case they are usable after all.
DECLARE #d_date DATE = '2008-07-01'
DECLARE #v_date VARCHAR(25) = '2008-07-01'
SELECT #d_date,
#v_date,
DATEADD(YEAR, 8, #d_date),
DATEADD(YEAR, 8, CONVERT(DATE, #v_date)),
DATEADD(YEAR, 8, CAST(#v_date AS DATE)),
CAST(CAST(LEFT(#v_date, 4) AS NUMERIC) + 8 AS VARCHAR(4)) + RIGHT(#v_date, LEN(#v_date) - 4)
Please do not use the last one.
An example of Code.
http://rextester.com/NHHL75120

SQL Server 2014 trying to create an indexed view however I'm getting the following error

SELECT
fcb.PharmacyKey as PharmID
,fcb.DateKeY as UploadDate
,fcb.RecordSource as Description
,fcb.Amount
FROM
dbo.FactCheckBookData fcb
WHERE
fcb.RecordSource in ('BeginningBalance')
AND convert(datetime, left(fcb.DateKey, 8),101) = DATEADD(MONTH, DATEDIFF(MONTH, 0, Convert(varchar(8), getdate(), 112)), 0)
Error:
Cannot create index on view 'dbo.view' because the view uses an
implicit conversion from string to datetime or smalldatetime. Use an
explicit CONVERT with a deterministic style value.
You can add one day to end of previous month.
SELECT
fcb.PharmacyKey as PharmID
,fcb.DateKeY as UploadDate
,fcb.RecordSource as Description
,fcb.Amount
FROM dbo.FactCheckBookData fcb
WHERE fcb.RecordSource in ('BeginningBalance')
AND convert(datetime,left(fcb.DateKey,8),101) =
DateAdd (Day, 1, EOMONTH(getdate(), -1))
As mentioned in the comments, to find the first day of the month, you can use
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0)
However, if you change your code, the error will change to
Cannot create index on view 'someView'. The function 'getdate' yields nondeterministic results. Use a deterministic system function, or modify the user-defined function to return deterministic results.
Because GETDATE() is non-deterministic and therefore cannot be used in an indexed view.
https://msdn.microsoft.com/en-AU/library/ms191432.aspx
https://msdn.microsoft.com/en-us/library/ms178091.aspx

Convert date to SQL datetime

I'm somewhat new to T-SQL and despite reading a number of articles that indicate this should work, I'm having trouble converting October 1st of the current year to a datetime.
I've tried:
SELECT CAST(DATEPART(year, GETDATE()) + '1015' AS DATETIME)
SELECT CONVERT(datetime, 'Oct 15 ' + DATEPART(YEAR,GETDATE()),100)
And all kinds of variations.
Any ideas? I need to set a datetime variable to whatever Oct 1st of the current year is.
What you're trying to is close, but DATEPART returns a number, so the "+" is doing addition, not concatenation.
Try it like this:
SELECT CAST(CAST(DATEPART(year, GETDATE()) AS VARCHAR(4)) + '1015' AS DATETIME)
edit -- Ed beat me to it, and the Concat function is better too.
But if you really wanted to knock it out of the park, try this...
SELECT DATEADD(month, 9, DATEADD(year, DATEDIFF(year, 0, getdate()), 0)) As October1CurrentYear
No casting required!
Your first query is very close. The problem is that the plus sign (+) for concatenation is actually giving you a numeric value, which you can't cast to a date.
To concatenate a year and '1015' and end up with a string, use the CONCAT function instead:
SELECT CAST(CONCAT(DATEPART(YEAR, GETDATE()), '1015') AS DATE)

Howto convert datetime to date with an ODBC function?

Question:
I need to get the DATE ONLY (= date WITHOUT time) with an ODBC escape sequence.
However
SELECT
{fn CONVERT(SomeTable.Col_With_DateTime_Value, SQL_DATE)}
FROM SomeTable
does return the column as datetime value (date WITH time).
Is there any ODBC function I can use to get the date value only ?
Note:
This is not a duplicate question.
I know one can use the non-ODBC convert function, like
CONVERT(char(8), getdate(), 112) AS v112_ISO
CONVERT(char(10), getdate(), 104) AS v104_XML
but I really need an ODBC function for compatibility reasons.
You could use this:
select {fn convert({fn timestampdiff(SQL_TSI_DAY, 0, DatetimeCol)}, SQL_DATE)}
from SomeTable
Works in the environments I have available to test on (SQL Server 2000-2012).
Update:
Here is another way that I believe could be faster than using convert.
select {fn timestampadd(SQL_TSI_DAY, {fn timestampdiff(SQL_TSI_DAY, 0, DatetimeCol)}, 0)}
from SomeTable
select cast(day(getdate()) as varchar(2))+'/'+cast(month(getdate()) as varchar(2))+'/'+cast(year(getdate()) as varchar(4))
or
SELECT CONVERT(VARCHAR(10),GETDATE(),111)