Currently I have tables that I am trying to join together by dates.
One of them returns a timestamp (2020-12-21 00:13:56.312) and the other 12/21/20. I am trying to make it so I can join these two by date. I am doing this in snowflake and it does not seem that I can use the format() function. Thanks
In Snowflake:
TO_CHAR(timestamp, 'MM/DD/YY')
You could use to_timestamp():
where mytimestamp = to_timestamp(mystring, 'mm/dd/yy')
Related
I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.
I'm working on JupyterLab(SQL) and I want to get the difference of days between two columns.
The values of the columns are in the format YYYYMMDD but they aren't integers
How can I transform the columns to dates and then get the differences of days.
I'm not totally sure about JupyterLab, but in SQL Server you can use DATEDIFF() to calculate this. For example:
SELECT DATEDIFF(day, '2017/08/25', '2011/08/25') AS DateDiff;
See also:
https://www.w3schools.com/sql/func_sqlserver_datediff.asp
You did not mention the dbms so i am answering for oracle and postgres. You can use above answers but i like to convert them explicitly befor calculating difference.
so here is for
oracle -
TO_DATE('20170103','YYYYMMDD') - TO_DATE('20200103','YYYYMMDD')
postgres
EXTRACT(DAY FROM TO_TIMESTAMP('20160101', 'YYYYMMDD')-TO_TIMESTAMP('20150301', 'YYYYMMDD')
JupyterLab(SQL) supports SQLite, PostgreSQL, and MySQL databases:
MySQL:
SELECT DATEDIFF("20201005", "20201001");
PostgreSQL:
SELECT DATE_PART('day', AGE('20201005', '20201001'));
Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')
In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php
I want to format existing dates in a column called mydate in a table called empl. However, I am going no where with this. I have tried
SELECT CONVERT(VARCHAR(10), mydate,103) from empl
but get a missing expression error.
I want to change the current format in the column which is in the format
dd-mm-yy to mm/dd/yy
EDIT: im using oracle
Any suggestions?
If the datatype of the column is DATE, you can use the TO_CHAR() function:
SELECT to_char(mydate, 'mm/dd/yy')
FROM empl ;
Are you able to change the column type to date? Then you could use date_format
The Convert statement you show is for MS-SQL, and you would want code 3 not 103, the 100 series give 4 digit years.
Try:
select to_char(mydate,'MM/DD/YY') from empl
Assuming mydate is stored in date datatype, and using oracle, easy way to do that is:
select to_char(mydate, 'dd/mm/yy') from empl
In Oracle, convert is used to change character set with parameters
(char, destination_char_set, source_char_set)
select TO_CHAR(sysdate, 'MM/DD/YYYY') CHRDATE from dual;