Archive

Archive for the ‘SQL’ Category

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

January 27th, 2009 fbis 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 fbis 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 fbis 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: