2012-06-28

TSQL - Greatest of two dates using arithmetic


declare @val1 as date;
declare @val2 as date;

set @val1='2011-12-31';
set @val2='2012-06-27';

select
       DATEADD
       (
              d
              ,
              cast(
                     0.5
                     * (
                           (
                                  datediff(DAY,'1990-01-01',@val1)
                                  + datediff(DAY,'1990-01-01',@val2)
                           )
                           + ABS(
                                  datediff(DAY,'1990-01-01',@val1)
                                  - datediff(DAY,'1990-01-01',@val2)
                           )
                     )
                     as int
              )
              , cast('1990-01-01' as date)
       )
;

TSQL - Greatest of two numbers using arithmetic


declare @val1 as float;
declare @val2 as float;
set @val1=3.1;
set @val2=3;
select 0.5 * ((@val1 + @val2) + abs(@val1 - @val2));


TSQL - Lowest of two dates using arithmetic

You could create a UDF for this, and then nest calls to it in order to get the maximum of a collection of 3, 4, 5 or more fields.  Change the minus sign (big bold red below), to a plus sign, in order to retrieve the maximum of the two dates.


declare @val1 as date;
declare @val2 as date;

set @val1='2011-12-31';
set @val2='2012-06-27';

select
       DATEADD
       (
              d
              ,
              cast(
                     0.5
                     * (
                           (
                                  datediff(DAY,'1990-01-01',@val1)
                                  + datediff(DAY,'1990-01-01',@val2)
                           )
                           - ABS(
                                  datediff(DAY,'1990-01-01',@val1)
                                  - datediff(DAY,'1990-01-01',@val2)
                           )
                     )
                     as int
              )
              , cast('1990-01-01' as date)
       )
;

TSQL - How to get the lowest of two values using arithmetic

declare @val1 as float;
declare @val2 as float;

set @val1=3.1;
set @val2=3;

select 0.5 * ((@val1 + @val2) - ABS(@val1 - @val2));


2012-06-19

Facebook - How to add a share button

As of June 2012, you can use a simple link that follows this form. 
Of course, the link can be added to your page as simple text or be offered through a graphical icon.

http://www.facebook.com/sharer/sharer.php?v=4&src=bm&u=http%3A%2F%2FDOMAIN.COM&t=TITLE
 

2012-06-01

Teradata - All tables and column names list

This works both on tables and on views:


select
      *
from
      DBC.columns
where
      DatabaseName = 'DATABASE_NAME_HERE'
      and TableName like '%TABLE_NAME_HERE%'
      and ColumnName like '%COLUMN_NAME_HERE%'
;


HELP COLUMN DATABASE_NAME_HERE.TABLE_NAME_HERE.*;