varchar To Date Postgresql - postgresql

I I'm tryng to convert a varchar (04/07/2025)into a date 2025-07-04 but I've an error when find 31/09/2024 and return out of range value
select
id,
documento,
case
WHEN documento = null
THEN null
WHEN documento = ''
THEN null
WHEN documento not like '%%/%%/%%%%'
THEN null
--WHEN documento not like '%[0-9][0-9][0-9]-[0-9][0-9]-[0-9]%'
--then null
ELSE TO_Date(documento,'DD/MM/YYYY')
end as dt_scadenza_documento_id
from tab;
can you help me please?

Related

Select * with result columns that only contain values

postgresql
--I don't want to have to enter each repeating column, I want syntax in the where clause that checks all the columns and returns only columns with data
SELECT *
FROM table_with_200_columns
where datetime between '2021-01-01' and current_date
and column.1 is not null
and column.1 <>''
and column.2 is not null
and column.2 <>''
and column.3-200 is not null
and column.3-200 <>''
;
--something like this with an "presently unknown function" as in the example of 'allofthemtherecolumns'
SELECT *
FROM table_with_200_columns
where datetime between '2021-12-01' and current_date
and allofthemtherecolumns is not null
and allofthemtherecolumns <>''
your assistance is greatly appreciated
You can transform a row into an hstore record, then extract just the values and look if none of them is null
select * from t
where false = ALL (SELECT unnest(avals(hstore(t))) IS NULL);
PS: you would need the hstore extension
Convert the table row to json, remove datetime and all null/blank valued key, then check it's not empty:
select *
from table_with_200_columns
where datetime between '2021-01-01' and current_date
and regexp_replace(jsonb_strip_nulls(to_jsonb(table_with_200_columns) - 'datetime')::text, '"[^"]+":"",?', '', 'g') != '{}'

Use WHERE clause along with Between and NULL parameter for 2 date fields

Working with Parameters and SQL in a SSRS report
#FromDate and #ToDate can be null -- To get all 4 records
#Fromdate can have a date value while #ToDate is null - if this is the case need to get all date value which does not have TravelTo Date
#Fromdate and #todate has values then need to get the last 2 records
#FromDate and #ToDate is null then need to get all value
I used the below code to get the data but i am not getting the right data ,any suggestion is appreciated.
Where
(TravelTo BETWEEN #FROMSTART AND #TODATE)
or(#TODATE IS NULL and TravelFrom >=#FROMSTART )
OR(#FROMSTART IS NULL AND TravelFrom <=#TODATE)
OR(#FROMSTART IS NULL AND #TODATE IS NULL)
Parmater screenshot from SSRS
Assuming the either the date inputs or the columns themselves could be NULL, you may try:
SELECT *
FROM yourTable
WHERE
(#FROMSTART > TravelFrom OR #FROMSTART IS NULL OR TravelFrom IS NULL) AND
(#TODATE < TravelTo OR #TODATE IS NULL OR TravelTo IS NULL);
This logic would discount either the start or end of the date range should either the column have a NULL value, or the input have a NULL value.
SELECT *
FROM TABLE1
WHERE ((#FROMSTART IS NULL AND #TODATE IS NULL)
OR(#FROMSTART IS NULL AND TravelTo <=#TODATE)
OR(#TODATE IS NULL AND TravelFrom >=#FROMSTART)
OR(TravelFrom >= #FROMSTART AND TravelTo<=#TODATE))
Hope this Query Works fine for your case:
You had forgot to mentioned these conditions within an Open/Close bracket.So that only you cannot get the right output.
I think the problem is the 2nd condition
#Fromdate can have a date value while #ToDate is null - if this is the case need to get all date value which does not have TravelTo Date
Please try the code below which does (I think) what you specified in your question. If this does not work, please supply a set of test dates and expected outcome for each test as some are a little ambiguous.
NOTE: I have used standard date format for the dates as these will not suffer from conversion to and from the MM/DD/YYYY format you are using.
DECLARE #t TABLE (ID int, TravelFrom date, TravelTo date)
INSERT INTO #t VALUES
(9759497, '2020-02-01', NULL),
(5226416, '2020-01-15', NULL),
(2731975, '2020-03-01', '2020-04-30'),
(2318260, '2020-01-01', '2020-02-28')
DECLARE #fromDate date = '2020-02-01'
DECLARE #toDate date = NULL
SELECT * FROM #t
WHERE
(TravelFrom >= #fromDate or #fromDate is null)
AND
(TravelTo <= #toDate or #toDate is null)
AND
(
CASE WHEN #toDate IS NULL THEN 1 ELSE 0 END
=
CASE WHEN TravelTo IS NULL THEN 1 ELSE 0 END
)

Specific column -- Error converting data type nvarchar to bigint

This is part of the T-SQL.I am getting below error. Can anyone guide me.The issue is because of the value column which is of nvarchar datatype
SELECT RuleID, SourceID, DataFileID, ChildCount, DP_State
FROM
(SELECT DP_State.RuleID, CAST(DP_State.SourceID AS VARCHAR(20)) AS SourceID, CAST(DP_State.DataFileID AS VARCHAR(20)) AS DataFileID, ChildCount, DP_State
FROM (
SELECT RuleID ,
RuleResultID ,
CASE WHEN ISNUMERIC(ISNULL([ResultValue], 0)) = 1 THEN
CAST(ISNULL([Value], 0) AS BIGINT)
ELSE
-1
END AS ChildCount,
Error I am getting :
Try this if you are using SQL Server 2012 or later
SELECT RuleID,
SourceID,
DataFileID,
ChildCount,
DP_State
FROM (
SELECT DP_State.RuleID,
CAST(DP_State.SourceID AS VARCHAR(20)) AS SourceID,
CAST(DP_State.DataFileID AS VARCHAR(20)) AS DataFileID,
ChildCount,
DP_State
FROM (
SELECT RuleID,
RuleResultID,
CASE
WHEN TRY_CONVERT(INT, ISNULL([ResultValue],0)) IS NOT NULL
THEN CAST(ISNULL([Value], 0) AS BIGINT)
ELSE - 1
END AS ChildCount,
)
)
You are implicitly casting to an integer by first checking [value] against 0 in ISNULL(). Only after that you are casting to an integer, instead try the following:
Evaluate against a string:
CAST(ISNULL([Value], '0') AS BIGINT)
Or first cast to an integer:
ISNULL(CAST([Value] AS BIGINT), 0)

Hive select outputs null values

I get the select output as null for the following Hive table.
Describe studentdetails;
clustername string
schemaname string
tablename string
primary_key map<string,int>
schooldata struct<alternate_aliases:string,application_deadline:bigint,application_deadline_early_action:string,application_deadline_early_decision:bigint,calendaring_system:string,fips_code:string,funding_type:string,gender_preference:string,iped_id:bigint,learning_environment:string,mascot:string,offers_open_admission:boolean,offers_rolling_admission:boolean,region:string,religious_affiliation:string,school_abbreviation:string,school_colors:string,school_locale:string,school_term:string,short_name:string,created_date:bigint,modified_date:bigint,percent_students_outof_state:float> from deserializer
deletedind boolean
truncatedind boolean
versionid bigint
select * from studentdetails limit 3;
Output :
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
I have used the following properties while creating the table.
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ("ignore.malformed.json" = "true")
And the following properties while selecting the data.
SET hive.exec.compress.output=true;
SET io.seqfile.compression.type=BLOCK;
SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
ADD JAR s3://emr/hive/lib/hive-serde-1.0.jar;
Thank you for the comments, I have found the solution for this.
The issue was that the column name in my json file and the column name that i used while creating the table was different.
When i synched the column names between the Hive table and the Json File the issue was resolved.
Thanks & Regards,
Srivignesh KN

Oracle SQL Trigger insert/update---

Please help me with the issue, I want to write a trigger where I can insert the values into new table whenever insert/update happens in source table.
Below is the table structure from where I want to fetch data into another table.
LISTING TABLE
Name Null Type
LISTINGID NOT NULL VARCHAR2(28)
LISTINGMANAGERID NOT NULL VARCHAR2(28)
MANAGEAVAILABILITYFLAG VARCHAR2(1)
AVAILABILITYTEXT VARCHAR2(2000)
AREAINQUIRYFLAG VARCHAR2(1)
COUNTRYTEXT VARCHAR2(50)
STATEPROVINCETEXT VARCHAR2(50)
CITYTEXT VARCHAR2(50)
CHECKINTIME VARCHAR2(10)
CHECKOUTTIME VARCHAR2(10)
TIMEZONEID VARCHAR2(20)
PERSONALLINKURL VARCHAR2(150)
GOLDSUBSCRIPTIONSINCE DATE
AUDITPASSFLAG NOT NULL VARCHAR2(1)
MGRONLINEFLAG NOT NULL VARCHAR2(1)
ADMINAPPROVALFLAG NOT NULL VARCHAR2(1)
DELETEDFLAG VARCHAR2(1)
LASTUPDATED NOT NULL DATE
UPDATEDBY NOT NULL VARCHAR2(28)
OCA NOT NULL NUMBER(38)
SUSPENDEDFLAG NOT NULL VARCHAR2(1)
POSSIBLEFEATUREDCITYFLAG NOT NULL VARCHAR2(1)
TOTALPHOTOS NUMBER(38)
SUSPENDEDDATE DATE
OFFLINEDATE DATE
CURRENCY VARCHAR2(3)
POINTCHARGE VARCHAR2(2)
AVERAGEOFREVIEWS FLOAT(126)
NUMBEROFREVIEWS NUMBER(38)
RENTALMODEL VARCHAR2(10)
NOTHANKS VARCHAR2(1)
DIGITALSIGN VARCHAR2(50)
Need result in below table with any update or new insert from listing table.
i)listingid should contain listingid's from listing table.
ii)OFFLINE COLUMN should contain data with below condition.
AuditPassFlag = 'Y' AND AdminApprovalFlag='Y' AND MgrOnlineFlag='Y' AND DeletedFlag is NULL AND SuspendedFlag !='Y'
iii)TIMESTAMP COLUMN lastupdated date from listing table.
LISTING_LASTUPDATE TABLE
Name Null Type
LISTINGID NOT NULL VARCHAR2(20)
IS_OFFLINE VARCHAR2(1)
TIMESTAMP TIMESTAMP(0)
Below the trigger I am have written:
Its not working properly for where condition which means Listing is online: apart from that its work fine.
CREATE OR REPLACE TRIGGER listingLast_updated
AFTER INSERT OR UPDATE
ON listing
FOR EACH ROW
DECLARE
L_Listingid VARCHAR2 (20);
L_ISOFFLINE VARCHAR2 (1);
LASTUPDATED_TIMESTAMP DATE;
BEGIN
SELECT SYSDATE INTO LASTUPDATED_TIMESTAMP FROM DUAL;
IF ( :NEW.AuditPassFlag = 'Y'
AND :NEW.AdminApprovalFlag = 'Y'
AND :NEW.MgrOnlineFlag = 'Y'
AND :NEW.DeletedFlag IS NULL
AND :NEW.SuspendedFlag != 'Y') THEN
L_ISOFFLINE := 'Y';
ELSE
L_ISOFFLINE := 'N';
INSERT
INTO LISTING_LastUPDATED (LISTINGID, IS_OFFLINE, LASTUPDATED_TIMESTAMP)
VALUES (:NEW.LISTINGID, L_ISOFFLINE, LASTUPDATED_TIMESTAMP);
END IF;
END;
You've written:
Need result in below table with any update or new insert from listing table.
That is not what your trigger does. Take another look at your IF statement. It simplifies to:
if ... then
...
else
insert ...
end if;
Your INSERT is in the ELSE clause. You will therefore execute the INSERT when the initial IF statement is true. Not "with any update or new insert".
There are a number of other things you can change:
The variable L_Listingid is never used, remove it.
There's no need to use SELECT INTO ... in order to assign a variable. This can be done as follows in the declaration block:
lastupdated_timestamp date := sysdate;
However, I see no particular need for this variable to exist, so you can remove it and simply use SYSDATE in your sole INSERT.
There's no need to execute an ELSE at all. Set the default value of L_ISOFFLINE to be N and then use that if the IF condition doesn't evaluate to be true.
Two cosmetic changes, there's no need to wrap the ANDs in brackets; I've added the trigger name to the END so it's clearer.
You've you've also written:
TIMESTAMP COLUMN lastupdated date from listing table.
You actually have a LASTUPDATED date in your LISTING table, which means you're not following your own guidelines. You should use the column instead.
Putting all this together, your trigger might look like this:
create or replace trigger listinglast_updated
after insert or update
on listing
for each row
declare
l_isoffline varchar2(1) := 'N';
begin
if :new.auditpassflag = 'Y'
and :new.adminapprovalflag = 'Y'
and :new.mgronlineflag = 'Y'
and :new.deletedflag is null
and :new.suspendedflag != 'Y' then
l_isoffline := 'Y';
end if;
insert into listing_lastupdated (listingid, is_offline, lastupdated_timestamp)
values (:new.listingid, l_isoffline, :new.lastupdated);
end listinglast_updated;