Novice to postgresql here. On 8.3 (not my choice, can't do anything about it for near future).
I'm selecting some "time" text strings from a table and inserting them into a view:
create or replace view test as (
select
case
when desc like '%opening%' then 'opening'
when desc like '%closing%' then 'closing'
else n/a
end as time_type,
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'closing' as closing_time,
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM') where time_type = 'opening' as opening_time
from source_table);
I get the error:
ERROR: syntax error at or near "as"
LINE 8: .../YYYY HH:MI:SS AM') where time_type = 'closing' as closing...
I've used this syntax before to create other views so I am confused. Is it because my where statements are incorrectly placed? But if I place them after the from, they will be applied universally, no, and that is not what I want.
Two expressions look like they should be case statements, e.g.:
case
when time_type = 'closing' then
to_timestamp(time, 'MM/DD/YYYY HH:MI:SS AM')
else null
end as closing_time
Convert the two projections beginning with to_timestamp to CASE statements as well.
Related
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
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
I am trying to compare a date and a varchar from a database. Imagine I have a table like this
CREATE TABLE Sample (
mydate VARCHAR(20),
iddummy INTEGER
)
What I need to do is:
Select mydate, iddummy from Sample Where mydate > '01-30-2016';
However, as mydate is a varchar(20), it does not work. So I tried this way
SELECT date(to_date(mydate, '01-25-2016')) as newdate, iddummy
FROM Sample
WHERE newdate < '01-30-2016'
Any ideas how to do that? I am getting Error Code -206
The "-206" is because an alias ("AS newdate") defined in the SELECT clause is not yet visible in the WHERE clause; when parsing a SELECT statement, the clauses are taken in this order: FROM is first, then WHERE, then GROUP BY & HAVING, then SELECT, finally ORDER BY.
You would have to repeat the definition of newdate in the WHERE clause, or better: use a CTE:
WITH dt AS (
SELECT date(to_date(mydate, 'MM-DD-YYYY')) as newdate, iddummy
FROM Sample
)
SELECT * FROM dt WHERE newdate < '01-30-2016'
You can try this
SELECT mydate as newdate, iddummy
FROM Sample
WHERE date(mydate)< '01-30-2016'
But this would throw you an "ERROR: An invalid datetime format was detected; that is, an invalid
string representation or value was specified" if your mydate contains a string which cannot be converted to date ex: '01-30-2016aa1232' etc
Try using this
SELECT DATE(CHAR(trim(mydate),10) as newdate, iddummy
FROM Sample
WHERE DATE(CHAR(trim(mydate),10) < '2016-01-30'
Error code 206 states that "Column does not exist in any table of the SELECT". May be
SELECT mydate(to_date(mydate, '01-25-2016')) as newdate, iddummy
FROM Sample
WHERE newdate < '01-30-2016'
Following is my oracle update query that`s throwing error--
Update table Set col1 = To_date(:dateFill, 'mm/dd/yyyy hh24:mi:ss') Where Fil1 = :ID;
dateFill = 01/05/2012,
ID= 15
this statement is executing in a procedure,
error -:ORA-01722: invalid number(date field)
Can someone tell me why is 'select To_date('01/05/2012 00:00:00', 'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like '05-JAN-2012 00:00:00'.???
Please suggest me some answers.
If dateFill = 01/05/2012, why are you specifying a date format that includes "hh24:mi:ss"?
An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number. Valid numbers contain the digits '0' through '9', with possibly one decimal point, a sign (+ or -) at the beginning or end of the string, or an 'E' or 'e' (if it is a floating point number in scientific notation).
to_date always work with 'character' ie
to_date(char[,'format'[,nls_lang])
Your variable 'dateFill' is of Number datatype. Cast or convert this 'dateFill'
field into varchar and things will work.
Can someone tell me why is 'select To_date('01/05/2012 00:00:00',
'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like
'05-JAN-2012 00:00:00'.???
how a date displays is down to your client / Nls date format setting:
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/20120
--------------------
05-jan-2012 00:00:00
SQL> alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss';
Session altered.
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/2012
-------------------
01/05/2012 00:00:00
SQL>
Help me Stackoverflow, I'm close to going all "HULK SMASH" on my keyboard over this issue. I have researched carefully but I'm obviously not getting something right.
I am working with a Julian dates referenced from a proprietary tool (Platinum SQL?), though I'm working in SQL 2005. I can convert their "special" version of Julian into datetime when I run a select statement. Unfortunately it will not insert into a datetime column, I get the following error when I try:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
So I can't setup datetime criteria for running a report off of the Stored Procedure.
Original Value: 733416
Equivalent Calendar Value: 01-09-2009
Below is my code... I'm so close but I can't quite see what's wrong, I need my convert statement to actually convert the Julian value (733416) into a compatible TSQL DATETIME value.
SELECT
org_id,
CASE WHEN date_applied = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_applied-729960,convert(datetime, '07-25-99')),101)
END AS date_applied,
CASE WHEN date_posted = 0 THEN '00-00-00'
ELSE convert(char(50),dateadd(day,date_posted-729960,convert(datetime, '07-25-99')),101)
END AS date_posted
from general_vw
SELECT
org_id,
CASE WHEN date_applied = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_applied-729960,convert(datetime, '07-25-99'))
END AS date_applied,
CASE WHEN date_posted = 0 OR date_applied < 639906 THEN convert(datetime, '1753-01-01')
ELSE dateadd(day,date_posted-729960,convert(datetime, '07-25-99'))
END AS date_posted
from general_vw
You're casting to char but want a datetime so that's one easy fix.
You were also using '00-00-00' as your minimum date, but the minimum TSQL date is '1753-01-01'. Alternatively you could use something like ('1900-01-01') but that would need a change to the "less than" date_applied comparer.
I've added a "less than" date_applied comparer too. I calculated this as "SELECT 729960 + datediff(day,convert(datetime, '07-25-99'), convert(datetime,'1753-01-01'))". Any number less than this would cause a date underflow.