Archive : Database

SQL Server Format Number – Function

4
Digg me

This is simple function which can format a given number as Comma delimited number.

ex : 98712345 as 98,712,345

Thanks to http://siccolo.blogspot.com/

(Attached is the original code which works on SQL Server 2000 and SQL Server 2005.)

(download) formatnumber

I have slightly modified it for SQLServer 2005.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
– =============================================
– Author:
– Create date:
– Description:
– =============================================
– Verify that stored procedure does not exist.
IF OBJECT_ID (‘fn_format_number’, ‘FN’ ) IS NOT NULL
    DROP FUNCTION fn_format_number

PRINT ‘Function fn_format_number is Deleted’
GO
CREATE FUNCTION fn_format_number (@int_value int ) returns varchar(20)
AS
BEGIN
 DECLARE @is_negative BIT
 SELECT @is_negative = CASE WHEN @int_value<0 THEN 1 ELSE 0 END

 IF @is_negative = 1
  SET @int_value = -1*@int_value

 DECLARE @return_value VARCHAR(20)
 SET @return_value = CONVERT(VARCHAR, ISNULL(@int_value, 0))

 DECLARE @before VARCHAR(20), @after VARCHAR(20)

 SET @before = @return_value
 SET @after=”

 – after every third character:
 DECLARE @i INT
 IF LEN(@before)>3
 BEGIN
  SET @i = 3
  WHILE @i>1 and @i < len(@before)
  BEGIN
   SET @before = substring(@before,1,len(@before)-@i) + ‘,’ + right(@before,@i)
   SET @i = @i + 4
  END
 END
 SET @return_value = @before + @after

 IF @is_negative = 1
  SET @return_value = ‘-’ + @return_value

 RETURN @return_value
END
GO
PRINT ‘Function fn_format_number is Created’
GO

How to Save Column Names with Results : SQL Server 2005

2
Digg me

Whether you are copying query output from SQL Server Management Studio or Saving result as .csv file, if you need to save the column name or column header along with result.. here is simple option you have to enable.

Goto Query > Query Options or Press CTRL + Shift + O

Goto Option Grid Under Results

Check the box which says “Include column headers when copying or saving the results”

Click OK.

Run the query and save result as .csv or copy paste to excel with column headers. :)

How to Save Query results as .CSV or Excel file using SQLServer 2005 Studio

0
Digg me

Here is the simple way to save query results as .csv or .xls  file using SqlServer Management Studio.

Execute your query

Example : Select * from AdventureWorks.Person.Address
Right Click on Left top corner

Select “Save Result As” to save as .csv file

OR

Select “Copy” and paste the result in an Excel sheet.

How to Query Active Directory using LDAP in SQL Server 2000 / 2005 ?

6
Digg me

Step 1:- Add a linked server for the Active Directory
exec sp_addlinkedserver ‘ADSI’, ‘Active Directory Services 2.5′, ‘ADsDSOObject’, ‘adsdatasource’
Step 2 :- Then follow the steps mentioned here for setting up permissions.

Step 3 :- You can query the Active Directory listing (It lists only 1000 records. LDAP design is like that)
SELECT * FROM
OpenQuery(ADSI,
‘SELECT displayName, sAMAccountName, givenName,
telephoneNumber, company,department,mail
FROM ”LDAP://your LDAP server url”
where objectCategory=”person” and objectClass=”user” and  sAMAccountName = ”your login name’ ”)
Here is the list of columns that can be used to query.

If you get “Could not convert the data value due to reasons other than sign mismatch or overflow.”  error message nothing to worry.

Its a known bug, and you can request patch from Microsoft.  Google with the error message or you can check it out here.

http://support.microsoft.com/kb/887474

List of columns for querying Active Directory using LDAP.

1. countrycode
2. cn
3. msexchuseraccountcontrol
4. mailnickname
5. msexchhomeservername
6. msexchhidefromaddresslists
7. msexchalobjectversion
8. usncreated
9. objectguid
10. msexchrequireauthtosendto
11. whenchanged
12. memberof
13. accountexpires
14. displayname
15. primarygroupid
16. badpwdcount
17. objectclass
18. instancetype
19. msmqdigests
20. objectcategory
21. samaccounttype
22. whencreated
23. lastlogon
24. useraccountcontrol
25. msmqsigncertificates
26. samaccountname
27. userparameters
28. mail
29. msexchmailboxsecuritydescriptor
30. adspath
31. lockouttime
32. homemta
33. description
34. msexchmailboxguid
35. pwdlastset
36. logoncount
37. codepage
38. name
39. usnchanged
40. legacyexchangedn
41. proxyaddresses
42. userprincipalname
43. admincount
44. badpasswordtime
45. objectsid
46. msexchpoliciesincluded
47. mdbusedefaults
48. distinguishedname
49. showinaddressbook
50. givenname
51. textencodedoraddress
52. lastlogontimestamp
53. homemdb