Posts

Showing posts with the label Oracle11G

Convert XMLGregorianCalendar To Date I.e "MM/DD/YYYY Hh:mm:ss AM"

Answer : You can do this to return a Date : calendar.toGregorianCalendar().getTime() I found that code from this tutorial. From there, you can use a SimpleDateFormat to turn it into a string in the format you want. But, if you're using JDBC to save the date in the database, you probably can pass in the Date directly with this method: preparedStatement.setDate(colNum, myDate); Here is more clear answer: Get instance of Date from XMLGregorianCalendar instance: Date date = xmlCalendar.toGregorianCalendar().getTime(); I found that code from Convert XMLGregorianCalendar to Date in Java Format that Date instance with format "MM/dd/yyyy hh:mm:ss a", you will get MM/DD/YYYY hh:mm:ss AM format DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); String formattedDate = formatter.format(date) From Convert Date to String in Java For inserting database you would do what Daniel suggested If you want to insert your date on a database I would first do w...

Convert Timestamp To Date In Oracle SQL

Answer : CAST(timestamp_expression AS DATE) For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual; Try using TRUNC and TO_DATE instead WHERE TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD') Alternatively, you can use >= and < instead to avoid use of function in the start_ts column: WHERE start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD') AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD') Format like this while selecting: to_char(systimestamp, 'DD-MON-YYYY') Eg: select to_char(systimestamp, 'DD-MON-YYYY') from dual;

Creating An Oracle User If It Doesn't Already Exist

Answer : The IF NOT EXISTS syntax available in SQL Server, is not available in Oracle. In general, Oracle scripts simply execute the CREATE statement, and if the object already exist, you'll get an error indicating that, which you can ignore. This is what all the standard Oracle deployment scripts do. However, if you really want to check for existence, and only execute if object doesn't exist, thereby avoiding the error, you can code a PL/SQL block. Write a SQL that checks for user existence, and if it doesn't exist, use EXECUTE IMMEDIATE to do CREATE USER from the PL/SQL block. An example of such a PL/SQL block might be: declare userexist integer; begin select count(*) into userexist from dba_users where username='SMITH'; if (userexist = 0) then execute immediate 'create user smith identified by smith'; end if; end; / You need to write a pl/sql block. See an example here You can check if the user exists in the all_users table using som...