This algorithm requires an existing Prime numbers table. You can easily create one of your own or importing the primes ranging from 2 to 3,037,000,493 from the Internet. If you only is interested in primes with INT range {2.
Read more →
Today I am going to show a statement available with SQL Server 2008 and later, the MERGE command, and how it will be possible to insert into two tables within same statement.
Read more →
Here is a simple base converter that manages [from and to] base 2 to 16. CREATEFUNCTION dbo.fnBaseConvert ( @Value VARCHAR(8), @FromBase TINYINT, @ToBase TINYINT ) RETURNSVARCHAR(32) AS BEGIN RETURN ( SELECT SUBSTRING('0123456789abcdef', 1 +(x.
Read more →
Late this evening I stumbled across a post where OP wanted to get all IDENTITY values for the latest INSERT statement, and correctly OP stated that SCOPE_IDENTITY() only returns last IDENTITY value of the batch.
Read more →
Since this blog post http://weblogs.sqlteam.com/peterl/archive/2007/09/24/Finding-group-of-records-with-a-certain-status.aspx I have kept the technique in similar situations and perhaps never bothered to rethink my prerogative. Well, today I have. Recently I blogged here http://weblogs.
Read more →
Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-in-a-month.aspx is a specialized version for a month, and here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-number-of-weekdays-in-a-year.aspx is a specialized version for a year. This code below calculates the number of weekdays for any given period.
Read more →
Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-for-any-given.aspx you can find a specialized version that calculates number of weekdays for any given period. CREATEFUNCTION dbo.fnMonthWeekDays ( @Year SMALLINT, @Month TINYINT ) RETURNSTINYINT AS BEGIN RETURN ( SELECT 20 + SUM(1 - DATEDIFF(DAY, '17530101', dt) % 7 / 5) FROM ( SELECT DATEADD(MONTH, 12 * @Year - 22800 + @Month, -1) AS dt WHERE ISDATE(10000 * @Year + 100 * @Month + 31) = 1 UNION ALL SELECT DATEADD(MONTH, 12 * @Year - 22800 + @Month, -2) WHERE ISDATE(10000 * @Year + 100 * @Month + 30) = 1 UNION ALL SELECT CASE ISDATE(10000 * @Year + 100 * @Month + 29) WHEN 1 THEN DATEADD(MONTH, 12 * @Year - 22800 + @Month, -3) ELSE '18991231' END ) AS d WHERE @Year BETWEEN 1753 AND 9999 AND@Month BETWEEN 1 AND 12 ) END
Legacy Comments
Sowmya
2009-10-22
re: How to calculate the number of weekdays in a month Can u please explain the logic ?
Read more →
This function calculates the number of weeksdays in a year, and has error-checking for invalid years. It is also language independant. Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-for-any-given.aspx you can find a specialized version that calculates number of weekdays for any given period.
Read more →
It seems JDEdwards dates are stored NUMERIC(6, 0) in this format 107299, where 107 is the number of years after 1900, and 299 is the 299th day of the year. So how to convert JDEdwards to DATETIME?
Read more →
Finally, I am on vacation. I can't even remember when I had 4 week of contigous vacation last time! I wish the best to all of you, and see you soon again.
Read more →
Some time ago, I displayed how to work with XML data when searching for data stored in a XML column. Here Some XML search approaches and here Updated XML search (test case with variables).
Read more →
Today it happened. I received the Microsoft MVP Award for my contributions to the Microsoft SQL Server community. I am very honored by the award and I will continue to work hard for the community to keep their trust in me.
Read more →
I've posted a feedback on Microsoft Connect about how to enhance the INSERT INTO syntax. Especially for INSERT INTO ... EXEC ... Sometimes when you need the result from a stored procedure, the SP itself returns two (or more) resultsets.
Read more →
I've found an issue with the Debugger for SQL Server 2008 Management Studio a while ago. This is my way to ask you to endorse a fix https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=374183 Please let Microsoft know what you think about this suggestion.
Read more →
Recently I posted a function which returned the Nth weekday of a month, either from the beginning of month or from the end of month. Function is found here http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx I have fiddled around with it and have now extended the function to find the Nth weekday not only for a month, but also for a quarter or a year.
Read more →
You call this function with a date. The function returns a table with one record and 3 columns. First column is Weekday; Monday = 1, Tuesday = 2, Wednesday = 3; Thursday = 4, Friday = 5, Saturday = 6 and Sunday = 7.
Read more →
You call this function with three parameters: 1. Any date of the month in question 2. The weekday to calculate; Monday 1, Tuesday 2, Wednesday 3, Thursday 4, Friday 5, Saturday 6 and Sunday 7 3.
Read more →
I did some tests today to measure the different approaches for finding records present in one table but not in another. The results of CPU and Duration are presented below with some help from SQL Profiler.
Read more →
For some time ago, I posted an algorithm how to get the XML structure automatically. Today I stumbled across another approach which seems to be faster. Reservations though I haven't tested this against large xml data yet.
Read more →
A few months back, Adam Machanic launched a competition about "Grouped string concatenenation" here http://sqlblog.com/blogs/adam_machanic/archive/2009/02/27/t-sql-challenge-grouped-string-concatenation.aspx Now Adam has publish his results here http://sqlblog.com/blogs/adam_machanic/archive/2009/05/31/grouped-string-concatenation-the-winner-is.aspx and luckily my fourth suggestion was considered the overall winner!
Read more →