year coming wrong in TO_DATE function in db2 - database

I tried to run below query in DB2 database:
My date string: 122887 mmddyy
select DATE(TO_DATE('122887', 'mmddyy')) from SYSIBM.dual;
now result is: 2087-12-28
But i am expecting below 1987-12-28.
How to achieve this?

You need to use the "adjusted year" for your query. Instead of YY it is RR:
values(DATE(TO_DATE('122887', 'mmddrr')))"
1
----------
12/28/1987
Details are in the documentation for TO_DATE/TIMESTAMP_FORMAT.

Related

Snowflake - Setting Date Time format in result of Query

I'm running a query in snowflake to then export. I need to set/convert a date value to the following format 'yyyy-MM-ddThh:mm:ss' from 2022-02-23 16:23:58.805
I'm not sure what is the best way to convert the date format. I've tried using TO_TIMESTAMP, but keep getting the following error '1 too many arguments for function [TO_TIMESTAMP(FSA.LAST_UPDATED, 'yyyy-MM-ddThh:mm:ss')] expected 1, got 2'
This looks like a conversion issue. Please check datatype for your column last_updated. Also seems there is some typo in your question - for the time portion in format, use mi (hh:mi:ss).
Refer below -
select to_timestamp('2022-02-23 16:23:58.805'::TIMESTAMP,'yyyy-mm-dd hh:mi:ss.ff')
;
000939 (22023): SQL compilation error: error line 1 at position 7
**too many arguments for function
[TO_TIMESTAMP(TO_TIMESTAMP_NTZ('2022-02-23 16:23:58.805'), 'yyyy-mm-dd hh:mi:ss.ff')] expected 1, got 2**
select to_timestamp('2022-02-23 16:23:58.805'::string,'yyyy-mm-dd hh:mi:ss.ff');
TO_TIMESTAMP('2022-02-23 16:23:58.805'::STRING,'YYYY-MM-DD HH:MI:SS.FF')
2022-02-23 16:23:58.805
TO_TIMESTAMP is for string -> timestamp, TO_CHAR is for timestamp -> string of which the TO_CHAR( <date_or_time_expr> [, '<format>' ] ) form is the one you seem to be wanting.
this SQL show string -> timestamp -> formatted string
SELECT
'2022-02-23 16:23:58.805' as time_string,
to_timestamp(time_string) as a_timestamp,
to_char(a_timestamp, 'yyyy-MM-ddThh:mm:ss') as formating_string;
TIME_STRING
A_TIMESTAMP
FORMATING_STRING
2022-02-23 16:23:58.805
2022-02-23 16:23:58.805
2022-02-23T16:02:58

Google Sheets QUERY function dealing with dates

I have a table (gDisplay) in my GoogleSheets file that looks like this:
So, in another table I can run this query without pain:
=QUERY(gDisplay!A2:I;"SELECT sum(F) WHERE A > '2021-09-01' ")
But I need to fetch last week results, and I was guessig that the query below could work:
=QUERY(gDisplay!A2:I;"SELECT sum(F) WHERE A > date '"&TEXT(TODAY()-8,"yyyy-mm-dd")&"' ")
But it's not working and I had no lucky searching for the answer. Any clue to fix this?
after 8 you need semicolon not comma:
=QUERY(gDisplay!A2:I;
"SELECT sum(F)
WHERE A > date '"&TEXT(TODAY()-8; "yyyy-mm-dd")&"'")
but try:
=INDEX(QUERY(gDisplay!A2:I*1; "SELECT sum(Col6) WHERE Col1 > "&DATEVALUE(TODAY()-8)))

How to identify missing weeks using SQL code?

I am trying to identify specific weeks that are missing in my data. I have multiple states with different date ranges and would like to output all of the missing weeks for the various states that I do have. Not even sure where to begin with a SQL code that would begin to identify missing weeks. Any help on this would be greatly appreciated. BTW I'm using SQL Server 2016.
Thanks!
Sample Data:
State WeekEndingDate Sales
ID 7/5/2015 125000
ID 12/13/2015 127263
IN 8/20/2016 126589
IN 8/27/2016 124568
IN 10/15/2016 119654
MI 01/02/2017 105687
MI 02/05/2017 145962
An example of my desired output would be:
MI 01/09/2017 136589
MI 01/16/2017 125641
MI 01/23/2017 145769
MI 01/30/2017 135697
IN 09/03/2016 145693 and so on....
This is what's commonly known as a "gaps and islands" problem.
To solve, I strongly suggest you create a date table. Then, assuming that you have established that your week ending day is always Saturday, you can include that date table in the query.
select MD.state,DT.weekendingdate,MD.sales
from DateTable DT
left outer join MyData MD on DT.Weekendingdate = MD.Weekendingdate
where dt.weekendingdate >= '2016-08-01' and dt.weekendingdate <= '2016-08-31'

Solr query date not equal

I have a solrcloud schema with field that is path_hierarchy. Example data that it is storing,
"last_update": [
"/1/2015-09-22T10:59:46.000Z",
"/2/2015-09-23T10:59:46.000Z",
"/3/2015-09-24T10:59:46.000Z",
"/4/2015-09-28T10:59:46.000Z",
],
Is there a way for me to query where the date is not between 2015-09-23 to 2015-09-24?
Something like SELECT * FROM table WHERE date < 2015-09-23 or date > 2015-09-24 for SQL.
Try:
-last_update:[2015-09-23T23:59:59.999Z TO 2015-09-23T23:59:59.999Z+1DAY]
Yes, you can use range query and NOT operator; so you'll have something like
NOT date:[2015-09-23T23:59:59.999Z TO 2015-09-23T23:59:59.999Z+1DAY]
Please see https://wiki.apache.org/solr/SolrQuerySyntax

To_date function in Vertica format?

How can I get Vertica function to_date('','format') do output like this:
DDMonYYYY - 01/ABR/2012
and not like it does (01-04-2012)?!
dbadmin=> select now();
now
--------------------------------
19/09/2012 11:03:48.284339 BRT
(1 row)
dbadmin=> show datestyle;
name | setting
-----------+----------
datestyle | SQL, DMY
(1 row)
Try this:
SELECT TO_CHAR(CURRENT_TIMESTAMP, 'DD/MON/YYYY');
to_date() takes a string and converts it into a date object. The output you see returned is not formatted to the specified format. It is just an acknowledgement that the date object got created.

Resources