Dealing with dates in Oracle & Microsoft SQL

I’m not going to say much, the quoted comment from below explains it. Oracle pampers us.

From http://www.bennadel.com/blog/122-Getting-Only-the-Date-Part-of-a-Date-Time-Stamp-in-SQL-Server.htm

“I know I’m late to the party, but having dealt with both SQL Server and Oracle at length, it’s easy to say that date handling and manipulation in SQL Server leaves much to be desired. As evidenced by this thread, truncating dates in SQL Server oftentimes involves massive amounts of function nesting and string concatenation. Oracle offers a function named “trunc”, which does the same thing in a neat little package. The group I work for oftentimes deals with massive amounts of data, so we are all the time summarizing that data by day/month/quarter or even year in some cases. Remembering all the hoops to go through is just a mess, so I wrote a SQL Server equivalent to Oracle’s TRUNC function.

The function works like this:

fn_trunc(p_date,period)

For instance, if I want to truncate getdate() to today’s date:
select fn_trunc(getdate(),’d')

or the first day of this month:

select fn_trunc(getdate(),’m')

.. and so on. fn_trunc will truncate any date to the nearest minute, hour, day, month, quarter or year. The period argument accepts the same arguments the SQL Server DATEPART function uses, as well as the equivalent arguments used in the Oracle TRUNC function.

I’ll paste the code below, and please note that this implementation is for a system-wide function. Also note that I have not exhaustively tested the performance of this function call, so please anyone chime in if you have discovered a quicker way to skin this cat. I stayed away from string concatenation where possible, but prefer that over things like multiple DATEPART calls.

<code>
USE master
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON

EXEC sp_configure ‘allow updates’, 1
RECONFIGURE WITH OVERRIDE
GO

IF EXISTS (SELECT *
FROM dbo.sysobjects
WHERE uid = USER_ID(‘system_function_schema’)
AND name = ‘fn_trunc’)
DROP FUNCTION system_function_schema.fn_trunc
GO

/* ALL SYSTEM-WIDE FUNCTIONS MUST BEGIN WITH “fn_”"AND
MUST BE ALL lowercase
*/
CREATE FUNCTION system_function_schema.fn_trunc
(
@p_date datetime,
@p_period varchar(4) = ‘d’
)
returns smalldatetime
as
begin
declare @l_date smalldatetime

set @l_date =
case
when @p_period in (‘n’,'mi’)
then cast(convert(varchar(16),@p_date,120) as smalldatetime)
when @p_period in (‘h’,'hh’)
then cast(convert(varchar(14),@p_date,120)+’00′ as smalldatetime)
when @p_period in (‘d’,'dd’)
then cast(cast(@p_date as varchar(11)) as smalldatetime)
when @p_period in (‘w’,'wk’,'ww’)
then dateadd(dd, 1 – datepart(dw, convert(varchar(10), @p_date, 111)),convert(varchar(10), @p_date, 111))
when @p_period in (‘m’,'mm’)
then cast(convert(char(6),@p_date,112)+’01′ as smalldatetime)
when @p_period in (‘q’,'qq’)
then cast(cast(year(@p_date) as varchar(4)) + ‘/’ + cast(datepart(q, @p_date) * 3 – 2 as varchar(2)) + ‘/01′ as smalldatetime)
when @p_period in (‘y’,'yy’,'yyyy’)
then cast(convert(char(4),@p_date,120)+’-01-01′ as smalldatetime)
else
cast(cast(@p_date as varchar(11)) as smalldatetime)
end

return @l_date
end
go

EXEC sp_configure ‘allow updates’, 0
RECONFIGURE WITH OVERRIDE
GO
</code>

Leave the first comment

Online Meeting Software

http://www.fuzemeeting.com/

http://www.watchitoo.com/

Leave the first comment

Spool file header

set verify off feedback off pagesize 0 heading off linesize 4000 trimspool on trimout on wrap on serveroutput off arraysize 1

set

verify off

feedback off

pagesize 0

heading off

linesize 4000

trimspool on

trimout on

wrap on

serveroutput off

arraysize 1

Leave the first comment

Restart command

http://www.ubuntugeek.com/service-command-in-ubuntu-810-intrepid-server.html

sudo reboot

Leave the first comment

Oracle Automated Start

From http://download.oracle.com/docs/cd/B19306_01/server.102/b15658/strt_stp.htm#CFAHAHGA
2.2.2 Automating Database Startup and Shutdown on Other Operating Systems

To automate database startup and shutdown by using the dbstart and dbshut scripts:

Log in as the root user.

Edit the oratab file for your platform.

To open the file, use one of the following commands:

On Solaris:

# vi /var/opt/oracle/oratab
On AIX, HP-UX, Linux, and Tru64 UNIX:

# vi /etc/oratab
Database entries in the oratab file are displayed in the following format:

SID:ORACLE_HOME:{Y|N|W}
In this example, the values Y and N specify whether you want the scripts to start up or shut down the database, respectively. For each database for which you want to automate shutdown and startup, first determine the instance identifier (SID) for that database, which is identified by the SID in the first field. Then, change the last field for each to Y.

You can set dbstart to autostart a single-instance database that uses an Automatic Storage Management installation that is auto-started by Oracle Clusterware. This is the default behavior for an Automatic Storage Management cluster. If you want to do this, then you must change the oratab entry of the database and the Automatic Storage Management installation to use a third field with the value W and N, respectively. These values specify that dbstart auto-starts the database only after the Automatic Storage Management instance is started.

Note:
If you add new database instances to the system and if you want to automate startup for them, then you must edit the entries for those instances in the oratab file.

Leave the first comment

Linux Display variable

export DISPLAY=`who -m | awk ‘{print $6}’ | sed -e ‘s/(//g’ -e ‘s/)//g’`:0

Leave the first comment

SCP

scp username@boxname:

Leave the first comment

Installing Oracle Express on 64 bit Ubuntu

http://littlebrain.org/2008/05/12/how-to-install-oracle-xe-in-ubuntu-64-bit/

http://forums.oracle.com/forums/thread.jspa?threadID=848385&tstart=45

This is nothing to worry about – update-rc.d: warning: /etc/init.d/oracle-xe missing LSB style header

After it’s been installed, configure it:

sudo /etc/init.d/oracle-xe configure

Configure stuff, I used all defaults I believe.

Next in order to run sqlplus to do anything you need to source the config file:

source /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/bin/oracle_env.sh

Which in turn gave me this error:

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/nls_lang.sh: 114: [[: not found

Which is entirely related to what shell that it is trying to run on. In the file definition you’ll find #! /bin/sh and if you change this to #! /bin/bash it will run properly. Fixed!

Now doing a – ‘which sqlplus’ should return a path to sqlplus like this:

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/sqlplus

SQLPLUS /NOLOG

CONNECT username/password

All of this so I can ‘Enable Remote HTTP Connection with SQL Command Line’

EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);

Now I have to reconfigure port forwarding on my router at home in order to remotely access the Oracle Admin Website and router.

To access the Oracle Admin Site remote I should be able to go to:

http://host:port/apex

Where host = my domain name or IP and port = the default 8080 port I setup

I need to port forward 8080 requests to the server but I can’t remotely change my router yet.

I currently have it forwarding port 80 to the server but in order to change the router remotely I’m supposed to access myip:80

So one of those two needs to change.

Change HTTP or FTP port via SQLPLUS

SQL> begin

2    dbms_xdb.sethttpport(’80′);

3    dbms_xdb.setftpport(’2100′);

4  end;

5  /

CONNECT username/password
All of this so I can ‘Enable Remote HTTP Connection with SQL Command Line’ EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);Now I have to reconfigure port forwarding on my router at home in order to remotely access the Oracle Admin Website and router.To access the Oracle Admin Site remote I should be able to go to:http://host:port/apexWhere host = my domain name or IP and port = the default 8080 port I setupI need to port forward 8080 requests to the server but I can’t remotely change my router yet.I currently have it forwarding port 80 to the server but in order to change the router remotely I’m supposed to access myip:80So one of those two needs to change.
Change HTTP or FTP port via SQLPLUS
SQL> begin 2    dbms_xdb.sethttpport(’80′); 3    dbms_xdb.setftpport(’2100′); 4  end; 5  /

So one of those two needs to change.

Change HTTP or FTP port via SQLPLUS

SQL> begin

2    dbms_xdb.sethttpport(’80′);

3    dbms_xdb.setftpport(’2100′);

4  end;

5  /

Leave the first comment

Sql Plus command

sqlplus -S username/pw@sid @sqlfile.sql outputfile.txt args

Leave the first comment

Vi shortcuts

http://www.eec.com/business/vi.html

vi

Leave the first comment