Tampilkan postingan dengan label SQL Server. Tampilkan semua postingan
Tampilkan postingan dengan label SQL Server. Tampilkan semua postingan

25 Okt 2013

DateDiff and DateAdd Excluding Weekends in SQL Script

Have you ever wonder how to count date differences and add date, but excluding weekends (Saturday and Sunday)? I stumbled on these 2 functions, which I prefer best to do the operation.


CREATE FUNCTION [VARS].[FN_DateAddBusinessDay]
(
    @InputDate DATETIME = NULL,
    @InputDays INT
)
RETURNS DATETIME
AS
BEGIN
    -- Declare the return variable here
    DECLARE @OutputDateTime DATETIME
    DECLARE @date DATETIME
    DECLARE @days INT
    DECLARE @weeks INT
    DECLARE @i INT = 0
    -- Add the T-SQL statements to compute the return value here
    SET @date = @InputDate
    SET @days = @InputDays
    IF @days >= 1
    BEGIN
        SET @days = @days - 1
    END
    
    IF DATENAME(dw, @date) = 'Saturday'
    BEGIN SET @i = 2 END
    ELSE IF DATENAME(dw, @date) = 'Sunday'
    BEGIN SET @i = 1 END
    SET @date = DATEADD(d, @i, @date)
 
    IF (@days < 0)
    BEGIN
        IF DATEPART(dw, @date) = 1
            SET @date = DATEADD(d, -1, @date)
        SET @weeks = (datepart (dw, @date) + @days - 6)/5
    END
    ELSE
    BEGIN
        IF DATEPART(dw, @date) = 7
            SET @date = DATEADD(d, 1, @date)
        SET @weeks = (DATEPART(dw, @date) + @days - 2)/5
    END    -- Return the result of the function
    SET @OutputDateTime = DATEADD(d, @days + (@weeks * 2), @date);
    RETURN @OutputDateTime;
 
END
 
GO


CREATE FUNCTION [VARS].[FN_DateDiffBusinessDay]
(
    @InputStartDate DATETIME = NULL,
    @InputEndDate DATETIME = NULL
)
RETURNS INT
AS
BEGIN
    -- Declare the return variable here
    DECLARE @OutputDays INT
    DECLARE @date DATETIME
    DECLARE @days INT
    DECLARE @weeks INT
    -- Add the T-SQL statements to compute the return value here
    IF DATEDIFF(DD, @InputStartDate, @InputEndDate) >= 0
    BEGIN
        SET @OutputDays = (SELECT
           (DATEDIFF(dd, @InputStartDate, @InputEndDate) + 1)
          -(DATEDIFF(wk, @InputStartDate, @InputEndDate) * 2)
          -(CASE WHEN DATENAME(dw, @InputStartDate) = 'Sunday' THEN 1 ELSE 0 END)
          -(CASE WHEN DATENAME(dw, @InputEndDate) = 'Saturday' THEN 1 ELSE 0 END))
    END
    ELSE
    BEGIN
        SET @OutputDays = 0
    END
    RETURN @OutputDays;
 
END

Enjoy, and hope it helps! Winking smile

6 Apr 2013

Attach SQL Database from Network Drive

One crazy question came up in mind, can I attach a database from network drive? There’s a lot of recommendation, that we can’t attach SQL Database. Why would we want it? Anyway no one would do that, unless they want a little bit hiccup in their system and mess up the whole thing? That’s true, but I want a solid answer. Can it be done?

The answer is it can be done. But again, on really strict rules, this is not recommended. Why would I want it? Because I have development VM (VirtualBox) and I need those big databases (say about ~20GB) to be exist on my system without copying it inside my Virtual Hard Disk which will take some time to copy. Again, it’s a development VM, so I could manage it and no one would care if my system crashed or database not exist.

So, here it is.

Attach Database from Network
  1. -- this command to turn on network share attach DB
  2. DBCC TraceOn(1807)
  3. -- attach it
  4. EXEC sp_attach_db @dbname = N'WSS_Content_85',
  5.    @filename1 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85.mdf',
  6.    @filename2 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85_log.ldf'
  7. -- this command to turn off network share attach DB
  8. DBCC TraceOff(1807)

Please do note that this command would likely not recommended, especially on production server. Thanks to Sumardi Soemartopo for blogging about this COOL tips, and this is the link to that page http://guozspace.wordpress.com/2009/02/26/microsoft-sql-server-how-to-attach-database-from-network-drive/.

30 Jan 2012

Simple Scheduled SSIS Package for Import Data

Well, next week will be a brand new workplace for me. This week means rest for me, and I’m happy with it. Anyway, I’ll share to you how to make a simple SQL Server Integration Service package to be scheduled every time. You don’t have to do a code, if your import job is very simple. I’ll show you more on next paragraph.

29 Okt 2010

Tips to Create a Stronger Password and Manage Admin Users in Your Company

Maybe this topic is to ridiculous to me, but sometimes, for other people, it’s very useful. I’ve traveled so much, meeting so much clients, and see how different company can loose it’s security, or tighten it up. Some company doesn’t realize that their company isn’t protected so much, and could hacked in anytime, and this company is big enough. So, these are the passwords that someone could lost his/her job just for let the default or common password reside in their admin users.

  1. pass@word1
  2. P@ssw0rd
  3. Sequential numbers forward and backward like 123456, or 12345678, or 87654321
  4. princess
  5. Sequential characters like abcdef
  6. Sequential characters in keyboard like qwerty, asdfghj, zxcvbnm
  7. Combination of sequential characters and numbers like abc123, or qwerty12345
  8. If the website name is RockYou.com, the password is sometimes rockyou

So stupid isn’t it? You could prevent all of hacking actions using a stronger password. Stronger password are containing lowercase and uppercase alphabetical and numbers, and special characters. See… I’m using clause “AND”, and you may think that “gILa$2(vk_q0” will meet the requirements, and you may think that password will be lost sometime, because of too hard to remember. And if you try to write it on a paper, someone could stole your paper and see the password.

Try to find some sentences, in a normal ways. If you could, try find it on another language, not in English. Some password cracker application could find the password on it’s library, and think that the password is in English, like “princess”. Try “Admin for Rock You.com”, or “Library Administrator on Department”, or use another language, like in mine, “password goblok”, it means “stupid password”.

And from that point, you could change the sentence into 1 word only and don’t make an obvious password and easy to read, using combination of alphabetical lowercase or uppercase, and numbers, and special characters. Example for “Admin for Rock You.com” is “4Dm1n4RY” or “My@dM1nR0cKY0u”. Or you can try “I don’t remember” changed into “1dnTR3m3m&er” as a password.

In that way, you can still remember the password but with better security.

How to Manage Admin Users?image

Try to disable the domain admin, and create a new user as a domain admin. The Domain Admin in Active Directory is “Administrator”, so if you in Microsoft Corp, maybe the login sentence like “MICROSOFT\administrator”. Before you disabled the Domain Admin, try to create a new standard user, for example “MICROSOFT\pegasus” or “MICROSOFT\superman”, and use the above tips to create password. Join this user to “MICROSOFT\Administrators”, “MICROSOFT\Domain Admins” groups, or any other groups equivalent to “MICROSOFT\Administrator”, and then you can disable the MICROSOFT\administrator”.

 

If you have any other products like SQL Server or SharePoint, or even ForeFront, or any products which require an administrator user, specify all of admin one by one. Don’t try to use “MICROSOFT\Administrator” or “MICROSOFT\superman” or anything equivalent to Domain Admins. For an example, specify “Microsoft\SQLServerAdmin” for SQL Server, “Microsoft\SPSAdmin” for SharePoint, etc.

Happy configuring…

Optimizing and Improving SharePoint Performance from SQL Server

So, there is some ways to optimize and improve SharePoint (any edition) from SQL Server. There’s some way to improve this SharePoint things, and from SQL Server is one another way to do that. The important key is, where is your MDFs (MDF is the file extension for SQL Server Data File) and LDFs (LDF is the file extension for SQL Server Log File) located? What happens if I lost my MDFs? Or what happens when I lost my LDFs? What path should I take to make SharePoint much faster?

19 Okt 2010

Impersonate Active Directory Account when Connecting to Database using Windows Authentication

Lately I have a little problem, not a major but I’m not comfortable and must get to used to it. Suppose we want to connect to another SQL Server Database, and we don’t have any options except use the WINDOWS Authentication. But frankly, this SQL Server which we want to connect to is in another domain. Yeah, you could try to use Windows Authentication, but the User name field and Password field are grayed out. For example, my notebook wasn’t joined to another domain (with logged on user is RADIT\raditz), and I need to connect to another SQL Server in a different domain (suppose another domain user to connect to is CONTOSO\admin), use the following command-line:

RUNAS /user:CONTOSO\admin /netonly “<the SQL Management Studio exe path>”

RUNAS /user:CONTOSO\admin /netonly “D:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe”

image

You’ll be asked for the password, and the application will running with that user accounts.

image

Don’t worry, just insert the SQL Server Name, and click Connect.