Casting a datetime column as date 'yyyy-mm-dd' in SQLdf - date

Running this query in SQLdf and when I go to view or filter based on date I'm noticing the date column is in datetime format instead of 'yyyy-mm-dd'. I tried casting the column as a date with the correct format but didn't work. is there a way to format it in the query or update that column in the data frame after?
BUS_SERV_PROMO_ROLL_FCST <-
sqldf(
"
SELECT
D.DATE
,D.DATA_ROLLS_FORECAST
,VO.VOICE_ROLLS_FORECAST
,VI.VIDEO_ROLLS_FORECAST
,D.DATA_ROLLS_UPPER_FORECAST
,VO.VOICE_ROLLS_UPPER_FORECAST
,VI.VIDEO_ROLLS_UPPER_FORECAST
,D.DATA_ROLLS_LOWER_FORECAST
,VO.VOICE_ROLLS_LOWER_FORECAST
,VI.VIDEO_ROLLS_LOWER_FORECAST
,D.DATA_ROLLS_FORECAST_TREND
,VO.VOICE_ROLLS_FORECAST_TREND
,VI.VIDEO_ROLLS_FORECAST_TREND
,D.DATA_ROLLS_UPPER_FORECAST_TREND
,VO.VOICE_ROLLS_UPPER_FORECAST_TREND
,VI.VIDEO_ROLLS_UPPER_FORECAST_TREND
,D.DATA_ROLLS_LOWER_FORECAST_TREND
,VO.VOICE_ROLLS_LOWER_FORECAST_TREND
,VI.VIDEO_ROLLS_LOWER_FORECAST_TREND
FROM
dataforecast as D
LEFT JOIN
voiceforecast as VO
ON VO.DATE = D.DATE
LEFT JOIN
videoforecast as VI
ON VI.DATE = D.DATE
"
)
enter image description here

Related

Observations of the same date between hours with timestamp variable at PostgreSQL

I´m trying to run a select to chose some data between hours from the same date. The code below shows the idea, but it isn´t work. My datahora variable is a timestamp in the following format: 'yyyy-mm-dd hh24:mi:ss", as 2023-02-13 15:30:30. How can I run that?
select tr.tick, tr.cot, tr2.cot, tr2.cot-tr.cot as dif
from tb_registros tr
join tb_registros tr2 on to_char(tr.datahora, 'yyyy-mm-dd') = to_char(tr2.datahora, 'yyyy-mm-dd') and to_char(tr.datahora, 'hh24:mi:ss') between '08:55:00' and '08:56:10' and to_char(tr2.datahora, 'hh24:mi:ss') between '09:10:00' and '09:11:10'
where tick = 'ANY'
It's better to operate on proper date/timestamp or time values rather than strings. Assuming the column datahora is really defined as timestamp casting it to a date will remove the time part and casting it to a time value will remove the "day":
select tr.tick, tr.cot, tr2.cot, tr2.cot-tr.cot as dif
from tb_registros tr
join tb_registros tr2
on tr.datahora::date = tr2.datahora::date
and tr.datahora::time between '08:55:00' and '08:56:10'
and tr2.datahora::time between '09:10:00' and '09:11:10'
where tick = 'ANY'
::date is Postgres' proprietary cast operator. If you prefer ANSI SQL, you can use cast(... as date) instead

BigQuery conditional date formatting based on the value

I need some help around BigQuery date formatting based on their values. The source data has STRING datatype and target datatype is DATE. I need to format the input dates based on their values as follow:
NULL value should remain NULL
Empty string("") should be convert to NULL
Date with YYYY-MM-DD format should remain as is
Date with MM/DD/YYYY format should be convert to YYYY-MM-DD format
Here's what I have done so far:
SELECT input_date, CASE WHEN input_date = '' THEN NULL ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM mytable
The above case statement fails when try to parse the dates with YYYY-MM-DD format. Here's the error I am getting:
How do I solve for the YYYY-MM-DD date format? Any feedback is appreciated.
You may try and consider below approach wherein you will need to add another WHEN and then use regex to match the YYYY-MM-DD string and then parse it to your desired date format as shown below.
with sample_data as (
select NULL as input_date,
union all select '' as input_date,
union all select '2022-05-21' as input_date,
union all select '05/25/2022' as input_date
)
SELECT input_date,
CASE WHEN input_date = '' THEN NULL
WHEN REGEXP_CONTAINS(input_date, r'^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$') THEN PARSE_DATE('%F', input_date)
ELSE PARSE_DATE('%m/%d/%Y', input_date) END AS output_date FROM sample_data
Output:
Consider below query:
WITH sample_data AS (
SELECT * FROM UNNEST(['', null, '2022-05-21', '05/25/2022']) input_date
)
SELECT *,
COALESCE(SAFE.PARSE_DATE('%m/%d/%Y', input_date), SAFE.PARSE_DATE('%F', input_date)) output_date
FROM sample_data;
Output results

Convert packed DB2 iseries value to YYYY-MM-DD

I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;

sqlite date comparison

I have column of type date time and values are getting stored in format 10-29-2011 08:25.
I would like to find out the rows only which are less then current date-time. What will be the condition for date comparison for current date and this date-time column field?
Thanks.
you could use the datetime function
SELECT * FROM mytable
WHERE mydate > datetime('now')
you can even make date operations
SELECT * FROM mytable
WHERE mydate > datetime('now','-15 days')

Date range in PostgreSQL

When I apply a date range to my query, is there anyway to display the dates used in the date range even if there is no data at those dates?
Suppose I use,
... where date between '1/12/2010' and '31/12/2010' order by date
What I want in my result is to show sum of all amount column until 1/12/2010 on that day even if there is no data for that date and also same for 31/12/2010.
Join with generate_series() to fill in the gaps.
Example:
CREATE TEMP TABLE foo AS SELECT CURRENT_DATE AS today;
SELECT
COUNT(foo.*),
generate_series::date
FROM
foo
RIGHT JOIN generate_series('2010-12-18', '2010-12-25', interval '1 day') ON generate_series = today
GROUP BY
generate_series;
Result:
0,'2010-12-18'
0,'2010-12-19'
1,'2010-12-20'
0,'2010-12-21'
0,'2010-12-22'
0,'2010-12-23'
0,'2010-12-24'
0,'2010-12-25'