Archive

Archive for the ‘SQL’ Category

CSV, CDF string – Multiple In by string IN(“1,2,3,4,5″) etc

January 27th, 2009 No comments
CREATE FUNCTION CSVTable(@Str VARCHAR(7000))
RETURNS @t TABLE (numberval INT, stringval VARCHAR(100), DateVal datetime)
AS
BEGIN
 
DECLARE @i INT;
DECLARE @c VARCHAR(100);
 
SET @Str = @Str + ','
SET @i = 1;
SET @c = '';
 
while @i <= len(@Str)
BEGIN
IF SUBSTRING(@Str,@i,1) = ','
BEGIN
INSERT INTO @t
VALUES (CASE WHEN isnumeric(@c)=1 THEN @c ELSE NULL END,
rtrim(ltrim(@c)),
CASE WHEN isdate(@c)=1 THEN @c ELSE NULL END)
SET @c = ''
END
ELSE
SET @c = @c + SUBSTRING(@Str,@i,1)
SET @i = @i +1
END
RETURN
END

Read more…

Categories: SQL Tags:

Essential SQL Server Date, Time and DateTime Functions

January 27th, 2009 No comments

Essential SQL Server Date, Time and DateTime Functions

(2/13/07 Update: Once again, some great feedback has given us an even better Date() function — and a Time() function as well.  Thank you Michael !)

Read more…

Categories: SQL Tags:

Get Largest date

January 27th, 2009 No comments
CREATE FUNCTION fn_greatest_date
   (@date_1 datetime, @date_2 datetime, @date_3 datetime)
RETURNS datetime
AS
BEGIN
    DECLARE @return_date datetime
    SELECT @return_date = MAX(dateval)
    FROM
    (    SELECT @date_1 AS dateval UNION ALL
        SELECT @date_2 UNION ALL
        SELECT @date_3
    ) dates
 
 <a href="http://www.fruitbatscode.com/sql/get-largest-date#more-9" class="more-link">Read more...</a>
Categories: SQL Tags: