dot-nugget

Code and Knowledgebase Site

Code and Knowledgebase Site for Rumery Enterprises, LLC. Tampa Bay area custom software developer

tsql row_number

clock March 14, 2010 17:37 by author

SELECT C.AccountNumber, C.CustomerID, SO.OrderDate,
    ROW_NUMBER() OVER (ORDER BY SO.OrderDate) AS OrderNumber

FROM Sales.Customer C
    INNER JOIN Sales.SalesOrderHeader SO ON C.CustomerID = SO.CustomerID

article found here



tsql divide by zero fix

clock March 14, 2010 12:28 by author
good example below on how to use the NULLIF() function
here

select sum((xtndprce-extcost)/NULLIF(xtndprce,0)) as ActualMargin

I used in a group by select


tsql search for text in all stored procedures

clock March 14, 2010 12:27 by author
select * from sysobjects where id in ( select ID from syscomments where ltrim(Rtrim([text])) like '%ProductInfo%') returns the names of procedures that contain the text string


TSQL mm/yyyy date format

clock March 14, 2010 12:23 by author
select right('00' + convert(varchar(2),month(getdate())),2) + '/' + cast(datepart(yyyy,getdate()) as varchar(4)) result 12/2009 will sort correctly


c# send mail function

clock March 14, 2010 12:18 by author

quick and easy way to send email to many to addresses.

public static void SendMessageEmail(string eTO, string From, string Subject, string message, bool isHtml)

{

MailMessage msg = new MailMessage();

char split = Convert.ToChar(";");

string[] emTo = eTO.Split(split);

foreach (string toAdd in emTo)

{

msg.To.Add(toAdd);

}

msg.From = new MailAddress(From);

msg.Subject = Subject;

msg.Body = message;

msg.IsBodyHtml = isHtml;

SmtpClient client = new SmtpClient("mail.yourserver.com");

try

{client.Send(msg);}

catch (Exception exc) {//do something to handle error here }}



tsql send email

clock March 14, 2010 12:17 by author

using existing sql mail profile.

 

EXEC msdb.dbo.sp_send_dbmail

@profile_name

= 'ReportsMail',

@recipients

= @emailto,

@body

= @body,

@blind_copy_recipients

= 'add addresses here',

@subject

= @subject;



TSQL calcualte number of business days between 2 dates

clock March 14, 2010 12:17 by author
CREATE FUNCTION dbo.GetWorkingDays  
(  
    @startDate SMALLDATETIME,  
    @endDate SMALLDATETIME  

RETURNS INT  
AS  
BEGIN 
    DECLARE @range INT; 
 
    SET @range = DATEDIFF(DAY, @startDate, @endDate)+1; 
 
    RETURN  
    ( 
        SELECT  
            @range / 7 * 5 + @range % 7 -  
            ( 
                SELECT COUNT(*)  
            FROM 
                ( 
                    SELECT 1 AS d 
                    UNION ALL SELECT 2  
                    UNION ALL SELECT 3  
                    UNION ALL SELECT 4  
                    UNION ALL SELECT 5  
                    UNION ALL SELECT 6  
                    UNION ALL SELECT 7 
                ) weekdays 
                WHERE d <= @range % 7  
                AND DATENAME(WEEKDAY, @endDate - d + 1)  
                IN 
                ( 
                    'Saturday', 
                    'Sunday' 
                ) 
            ) 
    ); 
END  
GO 


tsql substring and charindex example. first initial and last name

clock March 14, 2010 12:13 by author

The sql code below will take the first character and then the rest of the character s from the space index.

john doe will be returned as jdoe.  can use any delimiter in charindex.  Add one to the int that charindex returns and the len(column) function to get the rest of the values.

substring([full name],1,1) + substring([full name],CHARINDEX (' ' ,[full name]) +1,len([full name])) [UserName]



Sign in