I have this column called 'Date' that contains dates formatted this way: '20150101'.
I tried using sql substring, but when I run the query using 'date' function in sql, it doesn't work on the date format I have.
Here is the query I made:
SELECT (DATE ((SUBSTR(JOUR, 1, 4),
SUBSTR(JOUR, 5, 2),
SUBSTR(JOUR, 7, 2)))) As date
FROM TABLE
Any idea? I couldn't find anything similar to this date format! I found one that uses the convert function but it's not in StandardSQL or BigQuery
What about PARSE_DATE ?
SELECT PARSE_DATE("%Y%m%d", "20190101") as parsed;
For your table:
SELECT PARSE_DATE ("%Y%m%d", JOUR) As mydatecolumn from TABLE
Related
Executing this statement in Teradata SELECT EOMONTH(GETDATE(), -2) AS month_calculated outputs
Data Type GETDATE does not match a Defined Type name
What is wrong with this syntax?
The above syntax is for the SQL server, For teradata we have to use
SELECT ADD_MONTHS (DATE , -2);
DATE in Teradata will get the current date
ADD_MONTHS lets you add or subtract a number of months
I'm trying to use the below code to convert the 'date_time' values in my table to 10 characters. Currently, the date_time values are in the format '2020:09:08 10:00:00' but I want to get rid of the time portion of the values so that I'm only left with the date. I'm using DB Browser for SQLite and I thought I would have to use the varchar data type to complete this task but its not working. The output of the code is also shown below.
SELECT CAST(date_time AS VARCHAR(10)) FROM stocks;
I would just use string functions:
substr(date_time, 1, 10)
Your datetime values are not valid datetimes for SQLite which recognizes only the format 'YYYY-MM-DD hh:mm:ss'.
If you used the above format then all you need would be the function DATE():
SELECT DATE(date_time)
FROM stocks;
For your current format you must use string functions to extract the first 10 chars and then replace all : with -:
SELECT REPLACE(SUBSTR(date_time, 1, 10), ':', '-')
FROM stocks;
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')
I have a String column from a temp view, were dates values are stored in format of '2020/01/01'.
I do need to convert that field to a date value to be stored into a Delta table in 'YYY-MM-DD' format.
I've been trying different kind of SQL functions but none of them seems to works, as a result, every function retrieves a null value:
select to_date(dateField, 'yyyy-MM-dd'),
CAST(dateField as date),
etc..
from TMP_table
Every function results in a null value, and there's no information about that into Databricks documentation.
As Usagi said above, select to_date(dateField, 'yyyy/MM/dd') AS date works perfectly.
This for example :
concat (concat('201', (substring('A41020', 2, 1)), '-', (substring('B210906', 6, 2))
and the result is 2014-06.
What additional query I can use to change this string (2014-06) to date format while I retrieve the data?
For now, I have to change cell format in Excel when I am using data from this query.
Help me master
Use the below query..here i have set the date as 01,which you can change as per your requirement. Also you can use CONVERT or CAST function in this situation.
SELECT CONVERT(datetime,'201'+substring('A41020', 2, 1)+'-'+substring('B210906', 6, 2)+'-01')
OR
SELECT CAST(('201'+substring('A41020', 2, 1)+'-'+substring('B210906', 6, 2)+'-01')as datetime)
If it's a table column that you want to convert:
CONVERT(DATETIME,[Your-column])
If you want to convert the result of a select query:
CONVERT(DATETIME,[select query])
When you try to convert the string to datetime, the string should have year,month and date or else you will need to append whichever is missing.
In your case, your result is 2014-06 which is missing the date part.If you try to convert this,it will give you a conversion error.
You can convert the above string by adding a date part.
select convert(datetime,'2014-06-01');
It will give the result as below: