Eomonth function on SQL Server 2014 - sql

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

Related

Date Manipulation in SQL

I tried different functions to convert Datepart for year, month and day to a date but I am getting errors.
This is what I tried:
SELECT
CONVERT(datetime, CHAR(YEAR(GETDATE()) + CHAR(DATEPART(MM, '02')) + CHAR(DATEPART(DD, '28')));
When running this code, I get an error:
Conversion failed when converting date and/or time from character string.
In case I remove those single quotes on 02 and 28 then SQL will return null.
Thanks in advance
If 2012+ you could also try DateFromParts()
Select DateFromParts(2016,2,28)
Returns
2016-02-28
Try this instead:
SELECT CONVERT(datetime, DATENAME(YEAR, GETDATE()) + '-02-28')
You can't extract a day or month from a string such as '02'. But there is no need to do that anyway.
As you can read in the documentation on DATEPART, the second parameter should be a date parameter:
Returns an integer that represents the specified datepart of the specified date.
In your SQL statement you are misusing this function twice... following are invalid because the second parameter cannot be converted to a date type:
DATEPART(MM,'02')
DATEPART(DD,'28')
You can construct a date from its parts using the DATEFROMPARTS function in SQL Server 2012 and later versions:
Returns a date value for the specified year, month, and day.
Or if you need the time parts filled in as well, DATETIMEFROMPARTS
Returns a datetime value for the specified date and time.
In versions prior to SQL Server 2012, this can be done like this:
DATEADD(MM, (#year - 1900) * 12 + #month - 1 , #day - 1);

Sql Server Select Only Date Part From DateTime

I have problem in Sql Server for select only date part from DateTime.
The value of DateTime is 2014-05-01 00:00:00.000.
If tried this query I don't have error and the output is correct:
SELECT CONVERT(VARCHAR(10),'2014-05-01 00:00:00.000',110)
2014-05-01
If tried this other query in the doTable:
SELECT
TOP 100 *
FROM
[n].[a2].[DOTABLE]
WHERE
CONVERT(VARCHAR(10),data,110) > DATEADD(DAY, - 1, getdate())
ORDER BY
data DESC;
I have this error:
SQL Server Error Messages - Msg 242 -
The conversion of a char data type to a datetime data type
resulted in an out-of-range datetime value.
The version of SQL server is:
Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
Oct 14 2005 00:33:37
Copyright (c) 1988-2005 Microsoft Corporation
Standard Edition on Windows NT 6.1 (Build 7600: )
I suppose I'm not doing right but I know why.
I think the following is a better way to do what you want:
where date >= dateadd(day, 0, datediff(day, 0, getdate()) - 1)
This truncates the current date to midnight yesterday, which I am guessing is what you really want.
For your method, try using format 120:
SELECT TOP 100 *
FROM [n].[a2].[DOTABLE]
WHERE CONVERT(VARCHAR(10), data, 120) > DATEADD(DAY, - 1, getdate())
ORDER BY data DESC;
You can do this on both sides:
SELECT TOP 100 *
FROM [n].[a2].[DOTABLE]
WHERE CONVERT(VARCHAR(10), data, 120) > CONVERT(varchar(10), DATEADD(DAY, - 1, getdate()), 120)
ORDER BY data DESC;
This format is YYYY-MM-DD which is useful for comparisons.
Then, upgrade SQL Server, and use the date data type instead.
Verified your query in SQL server 2008. It's running fine may be a specific issue related to SQL server 2005 for conversion between varchar and date time.
You can add explicit conversion to date type here
SELECT
TOP 100 *
FROM
[n].[a2].[DOTABLE]
WHERE
CAST( CONVERT(VARCHAR(10),data,110) as datetime) > DATEADD(DAY, - 1, getdate())
ORDER BY
data DESC;
My suggestion is to use the following conversion to zero out the date part you don't requre (in this case time):
declare #n int = datediff(day, 0, [some_datetime_col]);
The above part will return the number of days since SQL Server's epoch time, as an integer. Then, to finish off the conversion:
select dateadd(day, #n, 0);
This adds back that number of days, returning a datetime with no time portion. To apply it to your example:
where
datediff(day, 0, data) > (datediff(day, 0, getdate()) - 1)
In your case, you don't need to do the conversion back to datetime as it's just a where clause; you can compare integers pretty efficiently and get the same result.
The added benefit of this method is that you can just as easily apply it to months (get first day of month, for example) and years. More care needs to be taken with weeks, but that's beyond the scope of this answer.
why you convert date to varchar?
try this query
SELECT TOP 100 *
FROM [n].[a2].[DOTABLE]
WHERE data > DATEADD(DAY, - 1, getdate())
ORDER BY data DESC;

'DATE' is not a recognized built-in function name

I wish to find all records of the current day. I have a field Date of type DATE.
I am getting error on sql server
'DATE' is not a recognized built-in function name.
on this line
(DATE(EnterDate) = CURDATE() )
As the error states, there is no DATE function in SQL Server 2008 or 2012 (you tagged both so I'm not sure which you're targeting). You can, however, cast to a date type in SQL Server 2008 and above:
WHERE EnterDate = CONVERT(date,GETDATE())
Note that there's no CURDATE function either, so I've translated that to GETDATE()
Use the following condition in your where cluase
WHERE CAST(DateColumn AS DATE) = CAST(GETDATE() AS DATE)
^------------ Your Column Name with `Date` or 'DateTime' data type
CURDATE() is a mysql function, In Sql-Server we have GETDATE() function to get current date and time.
More efficient one is
WHERE EnterDate > DATEADD(dd, -1, DATEDIFF(dd, 0, GETDATE()))
Thanks #D Stanley #marc_S and #Mihai
Finally I get it done by
WHERE EnterDate > Convert(DATETIME,Convert(varchar,DATEADD(DAY,0,GETDATE()),101))
This is not the exact answer but example how to trim the time part from the date time variable
CONVERT(VARCHAR(10),date_manufactured,10) =CONVERT(VARCHAR(10),#startdate,10))

Using sql DATEADD function in java

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

Please tell me what is error in my date comparison sql query

Please help me to find out error in my SQL query. I have created this query to compare dates
select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS datetime) BETWEEN
CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)
It's giving me the error:
incorrect near 'AS'
I am using SQL Server 2005.
You wrote case instead of cast in two instances.
If planStartDate is actually a date, then there is no need to cast it to a character column:
Select ...
From Joinplans jp
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)
Now, if planStartDate is storing both date and time data, then you might want to use something like:
Select ...
From Joinplans jp
Where planStartDate <= GetDate()
And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)
This ensures that all times on the last date calculated via the DateAdd function are included