How to change format of date using sqlplus - linux

i have problem with sqlplus couse
sqldeveloper gives me data like this:
22/03/09 52345
22/03/10 53462
22/03/11 26436
and sqlplus gives me data in this format
09-MAR-22 52345
10-MAR-22 53462
11-MAR-22 26436
Is any way to change name of month on number using an argument or set ... ?
LIKE:
sqlplus [arg] Login/pass#key << EOD

The quick answer is to set the NLS_DATE_FORMAT session variable:
alter session set nls_date_format = 'YYYY-MM-DD';
This will only work for dates. There are other settings for timestamps (NLS_TIMESTAMP_FORMAT) and timestamps with timezones (NLS_TIMESTAMP_TZ_FORMAT).
You can also set these variables as environment variables, and sqlplus should pick them up. You can also set these defaults in SQL Developer by going to Tools > Preferences > Database > NLS.
Check the Oracle documentation for more information.

Related

I needs to execute one sql query against two DBs in at a time and export the data to csv files [duplicate]

I have file1.sh file and which internally needs to execute one sql query against two Oracle DBs at a same time and needs to export date to csv fiiles, below is the sample shellscript which executes the query against two dbs.
....
#!bin/bash
set -X
sqlplus -S ${user1}#${DBCONNECTIONNAME_1}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${user1}#${DBCONNECTIONNAME_2}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile2.csv
sed 1d csvfile2.csv > file2noheader.csv
cat csvfile1.csv file2noheader.csv > ${Targetdirectory}/Expod.csv
....
But it does not connect to DB and does not execute any query and simply displays sqlplus manual as how to use the connection string, please let me know how to call one query against two dbs and execute them in parrallay and binds output to separate csv files.
A given sqlplus session can only connect to one db at a time, so your requirement 'at the same time' is essentially a non-starter. If 'at the same time' really means 'sequentially, in the same script, then you are back to fixing your connect string. And at that you 'have more errors than an early Mets game' (with apologies to any NY Mets fans).
First, your script indicates that your sqlplus command is the very first actual command following specification of your shell processor and 'set -x'. Yet you make heavy use of environment variables as substitutions for username, password, and connection name - without ever setting those variables.
Second, your use of an '&' in the command line is totally confusing to both me and the parser.
Third, you need to preceed your reference to the sql script with '#'.
Fourth, your order of elements in the command line is all wrong.
Try this
#!/bin/bash
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
Targetdirectory=<supply value here>
#
sqlplus -S ${orauser1}/${orapw1}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${orauser2}/${orapw2}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile2.csv
Or create a database link form one DB to other and then run both sqls in one db, one over DB link.
select * from tab1
union
select * from tab1#db_link

How to match SQL server functions in Hive

I am trying to write the Stored Procedure for SQL equivalent in Hive. I managed to translate the first two:
DECLARE #ReloadMonths as INT=15
set reloadMonths=15
DECLARE #Anchor_DT as DATE =EOMONTH(Getdate(),-1);
set anchor_dt=select last_day(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd')`)
But I am having troubles translating the following two:
DECLARE #YearMonth as INT=C_II.Common.FN_COM_DATEToYearMonth(#Anchor_DT);
set yearMonth=(anchor_dt,'yyyy-MM')
DECLARE #StartYearMonth as INT =ISNULL(#StartYearMonth_Inp,C_II.Common.FN_COM_DATEToYearMonth(DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)));
set startYearMonth=${hiveconf:${hiveconf:startYearMonth}};
Any ideas or suggestions?
your requirements was not much clear in the question. Also it seems that this function 'C_II.Common.FN_COM_DATEToYearMonth' is specific to your project and it's not a standard sql server function.
Lets breakdown it in step by steps:
If we run below statements in sql server:
DECLARE #Anchor_DT as DATE =EOMONTH(Getdate(),-1);
select #Anchor_DT;
It will give you date as: 2019-06-30
whereas the hive conversion you made for this is incorrect.
select last_day(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'))
It will give you last day of current month as '2019-07-31' so the right and equivalent coversion to sql server would be as:
select DATE_SUB(current_date(),DAY(current_date()));
This will return you date as :'2019-06-30'
The last two statements in your question was not very clear but looks like you are expecting below conversion.
select date_format('${hiveconf:anchor_dt}','yyyy-MM');
It will return as : 2019-06
"DECLARE #StartYearMonth as INT =ISNULL(#StartYearMonth_Inp,C_II.Common.FN_COM_DATEToYearMonth(DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)));"
I have converted above statement in sql server as shown below:
select format((DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)),'yyyy-MM');
This will return date in sql server as : 2018-04
Answer to your question:
create a hive script and save it on your hdfs location.(testdatehive.hql)
select date_format('${hiveconf:anchor_dt}','yyyy-MM');
select date_format(add_months('${hiveconf:anchor_dt}',-${hiveconf:reloadMonths}+1),'yyyy-MM');
Shell script:
#!/bin/bash
#Declare integer variable
declare -i reloadMonths=15
echo $reloadMonths
echo "Executing the hive query - get anchor date and store it in shell variable"
anchor_dt=$(hive -e "select DATE_SUB(current_date(),DAY(current_date()));")
echo $anchor_dt
echo "Pass anchor_date & reloadMonths to hive script"
hive --hiveconf anchor_dt=$anchor_dt --hiveconf reloadMonths=$reloadMonths -f hdfs://hostname/user/vikct001/dev/hadoop/hivescripts/testdatehive.hql
echo "Executing the hive query - ends"
Here is your shell output:
15
Executing the hive query - get anchor date and store it in shell variable
2019-06-30
Pass anchor_date & reloadMonths to hive script
2019-06
2018-04
Let me know if this works.

Node.js - Oracle DB and fetchAsString format

I am stuck on a problem and I am not sure what is the best way to solve it. I have a date column that I want to select and I want to fetch it as a string. Which is great, node-oracledb module has this option with fetchAsString mehotd. But it fetches the date like this for example 10-JAN-16 and I want to fetch it like this 10-01-2016. Is there a way to do that from the node-oracledb module, or I should modify the date after I get the result from the query?
UPDATE: I mean solution without to_char in the query and without query modifications
Check out this section of my series on Working with Dates in JavaScript, JSON, and Oracle Database:
https://dzone.com/articles/working-with-dates-using-the-nodejs-driver
The logon trigger shows an example of using alter session to set the default date format. Keep in mind that there is NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT, NLS_TIMESTAMP_TZ_FORMAT.
I only show NLS_TIMESTAMP_TZ_FORMAT because I convert to that type in the examples that follow as I need to do some time zone conversion for the date format I'm using.
Another way to set the NLS parameters is to use environment variables of the same name. Note that this method will not work unless you set the NLS_LANG environment variable as well.

Does Presto have the equivalent of Hive's SET command

It's very convenient to be able to set script variables. For example,
SET start_date = 20151201;
SELECT * FROM some_table where date = {$hiveconf:start_date};
Does Presto have this capability?
You can do this
WITH VARIABLES AS (SELECT VALUE AS VAR1, VALUE AS VAR2)
SELECT *
FROM TABLE CROSS JOIN VARIABLES
WHERE COLUMN = VAR1
Not yet.
Presto only have set session command for setting some presto properties during current session.
For example
SET SESSION distributed_join=true;
But presto can not set a variable and use it in following sql like hive does.
Not quite a script variable per say; but some UIs like DataGrip (probably DBeaver as well) emulate passing variables; e.g.:
where date = ${start_date}

How to change default date_format in MemSQL 4.0

Is there a way to change the default date_format in MemSQL - I'd like to change it to "%m/%d/%Y" instead of the default "%Y-%m-%d", so that i can load data in that format from csv files.
I got error message when try to set it.
mysql> SET ##GLOBAL.date_format = '%m/%d/%Y';
ERROR 1238 (HY000): Variable 'date_format' is a read only variable
Appreciate for any help.
Unfortunately MemSQL follows MySQL for a lot of system variables and as you can see in their docs the default date_format variable is unused: http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_date_format
Furthermore, currently MemSQL does not support transforming a particular column during a load.
Keep an eye out to some awesome solutions to this and other similar problems in our next release.

Resources