FileNet - SQL for a date property between specific hours - date

I would like to fetch documents that a property date hour is between midnight and 4 AM.
I tried this:
SELECT [This], [Date], FROM Folder_Type_1
WHERE DATEPART(hh,[Date]) >= 0
AND DATEPART(hh,[Date]) <= 4
ORDER BY Date
and
SELECT [This], [Date], FROM Folder_Type_1
WHERE CONVERT(VARCHAR(8),Date,108) between '00:00:00' and '04:00:00'
ORDER BY Date
But none of them is working when I test it in the SQL query builder in the FEM.
DATEPART and CONVERT are not recognised. What is the correct way to do it?
I didn't find anything interesting in this SQL syntax reference.
Thank you in advance!

You are trying to use T-SQL functions within Content Engine Query Language. While its syntax might look like SQL, it is actually not. Not to mention it is obviously not T-SQL.
As of today, it is not possible to accomplish what you want. TimeSpan function introduced in the version 5.1 allows some manipulations with date parts. Those, however, are not sufficient for your task. You might want to check TimeSpan documentation.

I have used the follwoing before:
where c.DateCreated >= 20130101T000000Z
This is a snippet from a query executed using the api an not the fem, but in principle this should be the same sql

Related

PostgreSQL TO_DATE date/time field value out of range

I am using TO_DATE in one of my PostgreSQL functions and it is throwing errors like date/time field value out of range: "2021901". This is happening for the months of January to September as I need to add zeros in front of them. So I tried to execute a simple select query there as follows as I am using the same syntax in function.
SELECT TO_DATE(2021::varchar||09::varchar||'01','YYYYMMDD')
This is also giving me the error
ERROR: date/time field value out of range: "2021901"
SQL state: 22008
Now if I change the month to October, November, or December it works fine, but for all the other months, it is showing this error. I am actually new to Postgres and not sure how to fix this. It would be very much helpful if someone can point me in the right direction. Thanks
If your input values are numbers (integer), another alternative is to use make_date()
make_date(2021,9,1)
A better and easier way would be to just provide the date correctly and use TO_DATE, so as example do this:
SELECT TO_DATE('2021-09-01', 'YYYY-MM-DD')
If you really want to go your way, you can force a leading zero by using TO_CHAR, like this:
SELECT TO_DATE(2021::varchar||TO_CHAR(09, 'fm00')||'01','YYYYMMDD')
But I recommend to take the first propose.

Error while finding the time difference in SQL Sybase using DATEDIFF function

I have the following query to find the difference between two dates in minutes.
SELECT DATEDIFF(MINUTE,'28.11.2019 09:23:41:202',GETDATE()) AS time_difference
But, I am getting the error
Converting '28.11.2019 09:23:41:202 'to timestamp is not possible
SQLCODE = -157, ODBC 3 State = "07006"
The value '28.11.2019 09:23:41:202' is obtained by using GETDATE() function in a previous query.
What is wrong here? any help?
UPDATE:
The query works if the value '28.11.2019 09:23:41:202' is changed to "2019.11.28" format. As mentioned above,
The value "'28.11.2019 09:23:41:202'" is obtained from using the same function GETDATE() in a previous query.
You need to utilise a standard date-time format, for it to work. For example, see following query:
select DateDiff(minute, '2019-11-28 08:12:34', GetDate()) as time_difference
Example from Official documentation:
SELECT DATEDIFF(minute, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

Select a datetime in Tsql, trim the milliseconds and return as a string

Problem: I want to select a date (stored as datetime) and return is as a string with the milliseconds trimmed off.
eg 2017-01-04 08:47:30.0000000 => "2017-01-04 08:47:30"
My current solutions:
I have got 3 statements which do the above:
Substring option
select
SUBSTRING(CONVERT(nvarchar,EventDate),0,20)
from EventsTable
Double convertions
select
CONVERT(nvarchar, CONVERT(datetime2(0),EventDate))
from EventsTable
Short nvarchar
select
CONVERT(nvarchar(19),EventDate)
from EventsTable
Allof the above solutions work and achieve my goal.
Question:
What is the best practice / most efficient way to achieve my goal?
Use a date style in your convert:
select CONVERT(varchar(19),EventDate,120)
from EventsTable
I would use this. It uses only one function, and sets the correct length for the varchar, and the format tells SQL Server the correct format you need.
Since style 120 is a standard you are sure that it the same no matter what localisation settings are on your DB or Session.
I ran all the three queries with the Execution Plan enabled with SQL Server 2008R2.
I received the cost equal in all of them ( 33% each).
All the three queries take the same time and resources.
However, the third one might help you with the less code being written.

Date Parameter to Cache SQL

I am very new to Cache. I am trying to develop a report with date parameters. When I issue the SQL command:
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) >= convert(sql_date,'08-01-2014' )
I get what I expect two records.
One of which is 10/1/2010 12:00:00 AM.
Then if I issue the command
SELECT TOP 2 ad.admission_date from system.admission ad WHERE convert(sql_date,ad.admission_date) <= convert(sql_date,'08-01-2014' )
I get no values returned?
When I issue the command
SELECT TOP 2 {fn convert('10-03-2010', sql_date) } FROM system.admission_data
I get two NULL values. Clearly I am confused about how Cache works.
I have found that if you use the standard ODBC format (yyyy-MM-dd) for the date you don't need to use the convert and it is much more efficient:
WHERE ad.admission_date <= '2014-08-01'
I formated date incorrectly. I have my code working now. Should look something like select top 2 convert(DATE, '10/03/2010 12:00:00 AM') .... and then I can actually do comparisons.

Best method for varchar date validation in Sybase (T-SQL)?

I have a stored procedure which takes as its parameter a varchar which needs to be cast as a datetime for later use:
SET #the_date = CAST(#date_string AS DATETIME)
I'm expecting the date string to be supplied in the format "DD-MON-YYYY", but in an effort to code defensively, if for some reason it can't be cast successfully, I want to default to the system date and continue. In PL/SQL I could use exception handling to achieve this and I could do this fairly easily with regular expressions too, but the limited pattern matching supported out of the box by Sybase doesn't let me do this and I can't rely on third party libraries or extensions. Is there a simple way of doing this in T-SQL?
NB: using Sybase ASE 12.5.3, there is no ISDATE function
I'm having a similar issue. You might be able to do something like this:
SET arithabort arith_overflow off
SET #the_date = CAST(#date_string AS DATETIME)
IF #the_date is NULL
set #the_date = getdate()
SET arithabort arith_overflow on
However, this doesn't work well in a select. It will work well in a cursor (boo) or in logic before / after a SQL batch.
My goodness, if the question was about Microsoft SQL Server then we'd have been in business!
Sybase, sadly, is a whole 'nother database these days, since about 1997, in fact, give or take a year.
If the input format simply has to be 'DD-MON-YYYY' and no exceptions, then I think a fair amount of validation was be achieved by slicing the input using SUBSTR(), after first doing some simple things, such as checking length.
I thought that recent releases of Sybase (SQL Anywhere 11, for example) have regular expression support, however, although it's been a while since I've had to suffer T-SQL. Some googling leaves me in rather more doubt.
Can't you do something like this:
SELECT #the_date = CASE #date_string
WHEN '[0-9][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9]'
THEN CONVERT(datetime, #date_string)
ELSE GETDATE()
END
?
Found this in the second result in Google when searching for "validate date string sql".
----Invalid date
SELECT ISDATE('30/2/2007')
RETURNS : 0 (Zero)
----Valid date
SELECT ISDATE('12/12/20007')
RETURNS : 1 (ONE)
----Invalid DataType
SELECT ISDATE('SQL')
RETURNS : 0 (Zero)
Make sure SQL Server knows the order of Days, Months and Years in your string by executing
SET DATEFORMAT mdy;
Did you try convert instead of cast?
select convert( datetime , #date_string )