Unable to Display Correct Date Format in 2nd Select list of UNION - teradata

I have a simple query UNION ALL from two tables. I am finding that several of my columns of the second table is blank where there should be data and that the dates are shown as whole numbers (where you can discern the dates, but in YYMMDD preceded by "1"). I have shortened the query for this question to six columns to start. I have also made sure the data types are correct and even change '' to NULL and vice versa just to get errors. Here is the query:
SELECT
PRIMARY_SERVICE_DATE AS "Common Date"
,'P' AS "Claim Type"
,PRI_MEMB_ID AS "Member ID"
,PRIMARY_SERVICE_DATE AS "Primary Service Date"
,NULL AS "Admit Date"
,NULL AS "Discharge Date"
FROM MAINDB.TABLECLAIMS
WHERE
PRI_MEMB_ID IN ('99999')
UNION ALL
SELECT
ADMIT_DATE AS "Common Date"
,'' AS "Claim Type"
,CAST(MEMBER_ID AS BIGINT) AS "Member ID"
,NULL AS "Primary Service Date"
,ADMIT_DATE AS "Admit Date"
,DISCHARGE_DATE AS "Discharge Date"
FROM MAINDB.TABLESCLAIMSSUMMARY
WHERE ADMIT_DATE between '2016-01-01' and '2020-12-31'
AND CAST(MEMBER_ID AS BIGINT) IN ('99999')
This image link shows the results and how I need to display.
enter image description here

In Teradata, the first query in the UNION [ALL] determines the data types of the result, and in this case NULL is being considered INTEGER so the dates from the second query are implicitly CAST to INTEGERDATE form (CYYMMDD). Whenever your first query includes constant values, you should CAST to the expected result data type. (Also when data types or lengths differ you may need to explicitly CAST the result in the first query to be sure the values are returned as intended.)
SELECT
PRIMARY_SERVICE_DATE AS "Common Date"
,CAST('P' AS CHAR(1)) AS "Claim Type"
,PRI_MEMB_ID AS "Member ID"
,PRIMARY_SERVICE_DATE AS "Primary Service Date"
,CAST(NULL AS DATE) AS "Admit Date"
,CAST(NULL AS DATE) AS "Discharge Date"
FROM MAINDB.TABLECLAIMS
WHERE
PRI_MEMB_ID IN ('99999')
…
I am assuming you want the result as a DATE, despite formatting the Excel as DateTime.
If you want TIMESTAMP(0) then you should CAST all 3 dates in the first query.

Related

Giving error in mysql Insert Query When I am insert any data but i have only name and email at start level

When I am insert any data but i have only name and email at start level
INSERT INTO `users`(`id`, `name`, `email`, `email_verified_at`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES (3,"text","xyz#gmail.com","","","","","")
But getting error like this
#1292 - Incorrect datetime value: '' for column 'email_verified_at' at row 1
enter correct value for the field email_verified_at which empty in the query.
set the type of email_verified_at to timestamp and choose the default value to current_timestamp and then you can leave blank that field in your query, and it is better I think.

sqlite3: "IN" works, "NOT IN" doesn't

This query works:
select id from wagtailcore_pagerevision
where id in (select live_revision_id as id from wagtailcore_page)
This query produces no results:
select id from wagtailcore_pagerevision
where id not in (select live_revision_id as id from wagtailcore_page)
(The difference is in vs. not in.)
... but I can very plainly see by inspection of the data that the result of the second query should be many hundreds of rows.
An index does exist on the live_revision_id column, and id of course is a primary key.

how to get a unqiue result sets in PL/SQL cursor?

I want use this procedure to display the username and moblephone number,the result sets is this when I use select :
declare enter image description here
when the procedure runs,I get this :
enter image description here
error ORA-01722: invalid number
ORA-06512: at "ABPROD.SHAREPOOL", line 24.
when I use unique or distinct in the cursor,nothing display.
the code source :
create or replace procedure sharepool (assignment in varchar2,myorgname in varchar2) is
rightid T_CLM_AP30_RIGHT.RIGHT_ID%type;
orgid t_clm_ap30_org.org_id%type;
begin
select t.right_id into rightid from T_CLM_AP30_RIGHT t where t.rightdesc=trim(assignment);
dbms_output.put_line(rightid||trim(myorgname)||assignment);
select t.org_id into orgid from t_clm_ap30_org t where t.orgname=trim(myorgname);
dbms_output.put_line(orgid);
declare
cursor namelist is select distinct a.username,a.mobile from t_clm_ap30_user a, T_CLM_AP30_RIGHT_AUTH t where a.user_id=t.user_id and t.right_id=rightid and t.poolorgrange=orgid ;
begin
for c in namelist
loop
dbms_output.put_line(c.username||' '||c.mobile);
end loop;
end;
end sharepool;
INVALID_NUMBER errors indicate a failed casting of a string to a number. That means one of your join conditions is comparing a string column with a number column, and you have values in the string column which cannot be cast to a number.
ORA-06512: at "ABPROD.SHAREPOOL", line 24
Line 24 doesn't align with the code you've posted, presumably lost in translation from your actual source. Also you haven't posted table descriptions so we cannot tell which columns to look at.
So here is a guess.
One (or more) of these joins has an implicit numeric conversion:
where a.user_id = t.user_id
and t.right_id = rightid
and t.poolorgrange = orgid
That is, either t_clm_ap30_user.user_id is numeric and T_CLM_AP30_RIGHT_AUTH.user_id is not, or vice versa. Or T_CLM_AP30_RIGHT_AUTH.right_id is numeric and T_CLM_AP30_RIGHT.right_id is not, or vice versa. Or T_CLM_AP30_RIGHT_AUTH.poolorgrange is numeric and t_clm_ap30_org.org_id is not, or vice versa.
Only you can figure this out, because only you can see your schema. Once you have identified the join where you have a string column being compared to a numeric column you need to query that column to find the data which cannot be converted to a number.
Let's say that T_CLM_AP30_RIGHT_AUTH.poolorgrange is the rogue string. You can see which are the troublesome rows with this query:
select * from T_CLM_AP30_RIGHT_AUTH
where translate (poolorgrange, 'x1234567890', 'x') is not null;
The translate() function strips out digits. So anything which is left can't be converted to a number.
"T_CLM_AP30_RIGHT_AUTH.poolorgrange is varchar2,and t_clm_ap30_org.org_id is numeric ."
You can avoid the error by explicitly casting the t_clm_ap30_org.org_id to a string:
select distinct a.username, a.mobile
from t_clm_ap30_user a,
T_CLM_AP30_RIGHT_AUTH t
where a.user_id = t.user_id
and t.right_id = rightid
and t.poolorgrange = to_char(orgid) ;
Obviously you're not going to get matches on those alphanumeric values but the query will run.

SQLite : Insert text with single quote got from a SQL request

I have already check all the forums about this issue and I found a solution for my first insert, I used the "double quoted" instead a single quote as follow:
insertGiftShop(2,"photo02","Modern City by night photo", "item-grumdy1l", "A view of Modern City''s skytrain",1,100, "","no","items",0)
The previous function is inserting my row at the beginning of the game. When I check the database, the value is stored like this : " A view of Modern City's skytrain ".
It works very well! Now I'm trying to get the stored information "A view of Modern City's skytrain" and insert it in another table such as following:
function insertInventory(id, code, name, src, desc, sale, qtyoninventory, price, usetxt, type)
local sql = "insert into inventory (id, code, name, src, desc, sale, qtyoninventory, price, usetxt, type) values (" .. id.. ",'" .. code .. "','" .. name .. "', '" .. src .. "', '" .. desc .. "', '" .. sale .. "',"..qtyoninventory..","..price..",'"..usetxt.."','"..type.."')"
db:exec(sql)
end
insertInventory(maxid+1,row_2.code, row_2.name, row_2.src, row_2.desc, "no",1,row_2.price,row_2.usetxt, row_2.type)
In this situation, I'm getting the row_2.desc (what is "A view of etc..") directly from a stored filed. BUT it doesn't work because it takes the "single quote"!
How can I tell "format" the row_2.desc in order to add a needed "double quote" when the text has single quote inside?
Edit:
According to your comment, I've change the way to "insert" data in my tables.
So, I tried this:
tx.executeSql("INSERT INTO inventory(id, code, name) VALUES(?,?,?)",[2, "photo02", "Modern City's skytrain"])
I've an error near "[". Is the syntax correct?
I used this, is it correct and prevent SQL injection?
db:exec([[INSERT INTO items(id, code, name, src, desc, sale, qty, price, usetxt, type, onscene, area) VALUES(1,"ls01","LETP", "item-lss", "LVP","no",0,100, "It this burger?", "items", "yes", "table02")]])
Never ever put string values directly into an SQL string!
This not only gives you formatting problems (as you have seen), but also allows SQL injection attacks.
Use parameters instead, then you don't need to escape quotes:
tx.executeSql("INSERT INTO MyTable(ID, Name, Description) VALUES(?,?,?)",
[2, "photo02", "Modern City's skytrain"]);

Selecting values between 2 dates

I am currently working on a website that offers tutoring services and I have been stuck on this issue quite a while..
I have 2 text boxes where the user chooses a start date and a finish date, when the user clicks view he would be suppose to see the results in a gridview. I am using FormParameters to insert the date into the query.
SelectCommand of the sqldatasource
SelectCommand="SELECT [Session].Session_num AS Num, [Session].Session_Time_Stamp AS Session_Date, Student.Student_First & ' ' & Student.Student_Last AS Student FROM (([Session] INNER JOIN Student ON [Session].Student_Num = Student.Student_Num) INNER JOIN Class ON [Session].Class_ID = Class.Class_ID) WHERE ((([Session].Session_Time_Stamp) Between #firstdate And #seconddate))">
Parameters of the SqlDataSource
<SelectParameters>
<asp:FormParameter Name="firstdate" Type="DateTime"/>
<asp:FormParameter Name="seconddate" Type="DateTime"/>
</SelectParameters>
This is executed when the user clicks the view button, it is where I set the values of the parameters and execute the sql select.
Dim fdate As DateTime = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")
Dim ldate As DateTime = Format(CDate(txtEndDate.Text), "MM/dd/yyyy")
gridTutor.SelectParameters("firstdate").DefaultValue = fdate
gridTutor.SelectParameters("seconddate").DefaultValue = ldate
gridTutor.Select(DataSourceSelectArguments.Empty)
gridTutorSessions.DataBind()
fdate and ldate are not empty, however after this is executed the query result is empty. Could it be that wrong methods to execute the select?
Edit: I realized the problem is probably with the query and DateTime format. When I transformed my textbox value to DateTime it put it like this #2/20/2014#. However, it doesn't return anything even if there are values between the two dates.
If anybody have an access query with DateTime I would like to see it. Thanks
I managed to fix it by formatting the date in my access database it's not the best solution but it is a fix for the situation
I believe you need to manually set fdate and ldate just before you do your 'selectparameters' (i.e. use "fdate = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")". According to the following, values asigned to Dim in the manner you have would not reflect the realtime values you want. See the following regarding VB: "You can assign a value to a variable when it is created. For a value type, you use an initializer to supply an expression to be assigned to the variable. The expression must evaluate to a constant that can be calculated at compile time.

Resources