Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
User Journal

Journal Journal: Bash variable handling 1

Since I still haven't found any comprehensive source for how the evil variables in bash can be truncated, clipped and so on so here's a short list for me mostly:

VAR=hello in all examples

String length: ${#VAR}
ex:
echo ${#VAR} #yields '5'

Substring: ${#VAR:[chars to remove from start]:[chars to return]}
Last parameter can be ignored if you want to the end ("${VAR:2}")
ex:
echo ${VAR:1:3} #yields 'ell'

Remove last character: ${VAR%?}
ex:
echo ${VAR%?} #yields 'hell'

Return last character: ${VAR:${#VAR}-1}
Bit messy, best I could find though
ex:
echo ${VAR:${#VAR}-1} #yields 'o'

To be continued...

User Journal

Journal Journal: SQL Trigger that turns insert into update

-- When something is INSERTed into this table and the post already exists, does an UPDATE instead

Create TRIGGER PriceManager.Trigger_PriceList_Insert
   ON  TheDB.PriceList
   INSTEAD OF INSERT
AS
BEGIN
    SET NOCOUNT ON;

    --Update if exists
    UPDATE [PriceManager].[PriceList]
       SET [SalesPrice] = isnull(inserted.[SalesPrice],PriceList.[SalesPrice])
          ,[OnRequest] = isnull(inserted.[OnRequest],PriceList.[OnRequest])
          ,[ChangedTime] = isnull(inserted.[ChangedTime],PriceList.[ChangedTime])
          ,[ChangedBy] = isnull(inserted.[ChangedBy],PriceList.[ChangedBy])
    from inserted
     WHERE inserted.[Company] = PriceList.[Company]
          and inserted.[ItemNumber] = PriceList.[ItemNumber]
          and inserted.[Date] = PriceList.[Date]
          and inserted.[Type] = PriceList.[Type]
          and inserted.[PriceListId] = PriceList.[PriceListId]

    --insert if don't
    insert into [PriceManager].[PriceList]
    select * from inserted
    where not exists (
        select 1 from [PriceManager].[PriceList] p2
        WHERE inserted.[Company] = p2.[Company]
          and inserted.[ItemNumber] = p2.[ItemNumber]
          and inserted.[Date] = p2.[Date]
          and inserted.[Type] = p2.[Type]
          and inserted.[PriceListId] = p2.[PriceListId]
        )
END
User Journal

Journal Journal: SQL function to calculate number of work days

This is a follow up on the previous post about easter day, the function getEasterDate is required...

This calculates the number of work days between two dates, using Swedish holidays.

This consists of two functions, one that calculates if a given day is a holiday (isHoliday()) and one that simply loops through the range of days given and checks if any is a holyday (numberOfWorkDays())

If anyone wants to convert this to another country or optimize this one, please tell me about it! I'd love to see it...

Create Function isHoliday(@date as datetime)
returns bit
as begin
    if DATEPART(dw, @date) in (1,7)
        or (DAY(@date)=1 and MONTH(@date) in (1,5)) --Jan 1, May 1
        or (DAY(@date)=6 and MONTH(@date)=6) --6 july, national day
        or (MONTH(@date)=12 and DAY(@date) in (25,26)) --xmas
        return 1
    declare @easter as datetime set @easter = dbo.getEasterDate(YEAR(@date))
    if DATEDIFF(day, @easter, @date) in (-2, 1, 39) --easter + ascention
        return 1
    return 0
end

go

Create function numberOfWorkDays( @fromDate as datetime, @toDate as datetime)
returns int
as begin
    declare @day as datetime
    declare @endday as datetime
    declare @dayCount int

    set @day = @fromdate
    set @endday = @toDate
    set @dayCount = 1

    if @endday @day begin
        declare @tempday datetime set @tempday=@day
        set @day=@endday
        set @endday = @tempday
    end

    while @day @endday begin
        if dbo.isHoliday(@day)=0
                set @dayCount = @dayCount + 1

        set @day = dateadd(day, 1, @day)
    end

    return @dayCount
end

User Journal

Journal Journal: SQL function for calculating easter date

A SQL function to calculate the easter date on any given year:

create function getEasterDate( @yr int )
returns datetime
As Begin

        declare @g int, @c int, @x int, @z int, @d int, @e int, @n int

        set @g = @yr % 19 + 1
        set @c = @yr / 100 + 1
        set @x = (3 * @c) / 4 - 12
        set @z = (8 * @c + 5) / 25 - 5
        set @d = (5 * @yr) / 4 - @x - 10
        set @e = (11 * @g + 20 + @z - @x) % 30

        if (@e = 24) or ((@e = 25) and (@g > 11)) set @e = @e + 1

        set @n = 44 - @e
        if (@n 31 begin
                set @g = 4
                set @n = @n-31
        end

        return cast( cast(@yr as varchar) + '-' + cast(@g as varchar) + '-' + cast(@n as varchar) as datetime)
End

Slashdot Top Deals

FORTRAN is not a flower but a weed -- it is hardy, occasionally blooms, and grows in every computer. -- A.J. Perlis

Working...