Friday 15 February 2013

SQL–Overloading !

I came across this recently while considering the best way to create a function to deal with a SQL statement that could accept a varying number of parameters.

SELECT * FROM YourTable WHERE TableId = 2 AND Param1 = '%' AND Param2 = '%';

It means you don't need to use an overload function. Just swap out blank parameters for wildcard characters, meaning you don't need to do anything with the query as well. You could quite easily do this with any database engine, from Access to SQL Server.

Tuesday 12 February 2013

Converting an Excel date into PHP timestamp

I was recently asked by a customer to upload a large dataset extracted from their old Microsoft Office based database system into their shiny new web-based application (Built by the guys at Zypher).

The data included a lot of date fields which needed to be converted into UNIX timestamp to be used by the PHP based front-end.

We used the formula below to convert the dates, it assumes the date is in cell ‘A2’.

=(A2-DATE(1970,1,1))*86400

I hope it proves useful.

Saturday 19 January 2013

Calculate percentages with decimals in SQL?

Working with SQL Server recently I needed to calculate a percentage using values in other fields.  I went with the obvious field1 / field2 * 100 but got only a result of zero ?

After talking to everyone’s friend Google I came across the following stackover question and answer. It had the following answer:

CONVERT(
    DECIMAL(5,2),
      ( 100 * CONVERT(DECIMAL(5,2),[field1]) / CONVERT(DECIMAL(5,2),[field2]) )
) AS MyPercentAnswer

I hope it proves useful to you.

With thanks to those who answered the questions.