Database: DB2 v9.5.301.436
Requirement: I need to find number of days in a month.
Code:
select day(last_day(created))
from tablename
Error:
[Error Code: -440, SQL State: 42884] DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LAST_DAY;FUNCTION, DRIVER=3.57.82. 2)
[Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-440;42884;LAST_DAY|FUNCTION, DRIVER=3.57.82
I checked the DB2 documentation, Which shows above function is available.
http://www-01.ibm.com/support/knowledgecenter/api/content/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_bif_lastday.html
the following works fine for me.
select day(last_day(current_timestamp))
from SYSIBM.SYSDUMMY1
are you sure created is a date?
The below code worked for me.
select day(date(date(created))-(day(date(created)) -1) days + 1 month -1 day) as maxdate , created from customer ;
Note: created column is a timestamfild
If created is a Date couln use below query.
select day(date(created)-(day(created) -1) days + 1 month -1 day) as maxdate , created from customer ;
last_day where introduced in 9.7. You can roll your own like:
create function myfun.last_day(d date)
returns date
return (d + 1 month) - day(d + 1 month) days
with t(d) as (
values date('2014-01-11')
union all
select d + 1 month
from t
where d<'2015-01-01'
)
select d, myfun.last_day(d) from t
Related
I'm trying to get records which between two dates. Below is my query
select * from my_data
where name = 'customer' AND
(time between (('2021-03-24'::date - '1 month'::interval) AND '2021-03-24'::date))
However I'm getting a syntax error
ERROR: syntax error at or near ")"
LINE 1: ...03-24'::date - '1 month'::interval) AND '2021-03-24'::date))
^
What is the correct way of writing the query to return rows 1 month older than a given date?
You can drop the parentheses:
select *
from my_data
where name = 'customer' AND
time between '2021-03-24'::date - '1 month'::interval AND '2021-03-24'::date;
The specific issue is that the range for between does not accept parens.
I have a table with datehourminute column with string datatype and formatted as 202012062302. I want to select the date and cast it to date, so I made a query that looks like this:
select cast(date_parse(created_date, '%Y-%m-%d') as date) created_date,
daily_user
from
(
select concat(substr(datehourminute,1,4),'-',
substr(datehourminute,5,2),'-',substr(datehourminute,7,2))
as created_date, sum(users) as daily_user
from "a-schema".a-table
group by 1
)
order by 1;
However, that query returns this error:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC
An error has been thrown from the AWS Athena client.
INVALID_FUNCTION_ARGUMENT: Invalid format: "(oth-er-)"
Any idea how to fix this? Thanks!
I am working on Access 2012 on win 7.
In a table, I need to change a year date format from '1-Jan-06' to 2006 and from '1-Jan-90' to 1990. I only need year.
This is my query
SELECT *,
CASE
WHEN CAST(right(year_date,2) , INT) <= 12
THEN 2000 + CAST(right(year_date,2) , INT)
ELSE 1900 + CAST(right(year_date,2) , INT)
END
FROM my_table;
I got error:
syntax error (missing operator) in the query expression of 'CASE -- END'.
Access SQL does not support CASE WHEN or CAST.
In Access, you can use IIf() and CInt() to do what you intended with CASE WHEN and CAST.
SELECT
IIf(CInt(Right(year_date, 2)) <= 12, 2000, 1900)
+ CInt(Right(year_date, 2))
FROM my_table;
I have table called TrainingMatrix and it has a column called ExpiryDate. I am trying to write an SQL statement which can show me all records of 2012 as following:
SELECT * FROM "TrainingMatrix" WHERE "ExpiryDate" - current_date < 0 AND EXTRACT(YEAR FROM TIMESTAMP "ExpiryDate") = 2012;
But this doesn't work, can any one help?
I am using PostgreSQL.
No timestamp word when using column:
... AND EXTRACT(YEAR FROM "ExpiryDate") = 2012 ...
If you have index on ExpiryDate column - it's better to avoid using functions and have exact date/timestamp range:
and "ExpiryDate">='2012-01-01' and "ExpiryDate"<'2013-01-01';
I'm writing a SQL query on a timesheet report. I need the report to return only the details for the week of the selected date.
E.g., if I pick 02/01/2012 (dd/MM/yyyy), then it should only return results between 02/01/2012 and 08/01/2012.
Thanks
SELECT
*
FROM
yourTable
WHERE
dateField >= #yourDate
AND dateField < #yourDate + 7
Some variations of SQL may have specific ways of adding 7 days to a datevalue. Such as...
- DateAdd(Day, 7, #date)
- DATE_ADD(#date, INTERVAL 7 DAYS)
- etc, etc
This option is both index friendly, and is resilient to database fields that have time parts as well as date parts.
You easiest is the equivalent of WEEK_OF_YEAR function in your SQL engine
But you can also use DATE_ADD
WHERE table.date BETWEEN target_date AND DATE_ADD(target_date,INTERVAL 7 DAY)
That depends on the database system you're using. MySQL has a function calles WEEK(), SQL Server can do something like this with the DATEPART() function:
MySQL:
SELECT
*
FROM table
WHERE WEEK(date_col) = WEEK('02/01/2012');
SQL SERVER:
SELECT
*
FROM table
WHERE DATEPART(WEEK, datecol) = DATEPART(WEEK,'02/01/2012');
SELECT * FROM table_name
WHERE date BETWEEN '1/02/2012' AND '1/08/2012';
you can replace the example date with your date and yourdate + 6.
Look here for example: http://www.roseindia.net/sql/sql-between-datetime.shtml