ingenious code ::

The yoel sommer weblog

Format numeric value to currency in T-SQL

Recently, I encountered a situation in which I needed to retrieve formated currency values directly from my database. As rule of thumb, in 95% of all cases, the data should be retrieved as a raw number (with no formatting) and should be formated in the client application, but in this situation I needed a formated string.

In most cases a query like this:

use AdventureWorks
go
select sum(TotalDue) from Sales.SalesOrderHeader

will return: 140707584.8246

In order to resolve this issue I create a small user defined function that formats the values and return it a well formated currency value.

alter function udf_CurrencyFormat  (@number money)
returns varchar(100) as
begin
return '$' + convert(varchar,cast(@number  as money),1)
end 

After deploying this function a query like this:

select dbo.udf_CurrencyFormat(sum(TotalDue))
from Sales.SalesOrderHeader 

Will Return: $140,707,584.82

Posted in SQL Server | No Comments »

The utility that saved the day.

It’s been a while since I published something on my Blog. What can I say? it was a crazy month. I was working like crazy on our corporate website. We launched the beta version two weeks ago and got a lot of positive feedback, so I am excited of the new possibilities I could have with the new website as a marketing tool.
Anyhow, Yesterday I did something out of the ordinary and assisted one of clients to upgrade his server which included upgrading his SQL Server 2000 to 2005. The upgrade went smoothly on the SQL Server (Surprise!), but then it came the turn of the applications that were installed on the server. One in particular was driving us crazy as the sa password of the SQL Server was hard coded into it (!). The only problem was, the client didn’t know the sa password, it wasn’t written down and of course the password is encrypted in the syslogins table (master database)..
After trying in my bag of tricks without any results, I consulted my friends at SQLServerCentral.com and they recommended a utility called SQL Crack from NGS Software.
This utility is used for checking the security of the passwords on your SQL Server. Well, my assumption was that who ever set the password didn’t put a strong password and I was right. It took the software 20 seconds to crack the sa password and save the day. Needless to say that we changed all the other passwords it could crack to a strong password

Posted in SQL Server | No Comments »