skip to main | skip to sidebar
Prabir Kumar Basak's Blog
RSS

Generate Unique Id using DateTime

Stored Procedure 0 comments

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



CREATE PROCEDURE [dbo].[sp_OrderId]
AS
BEGIN
DECLARE @Date        VARCHAR(50)
DECLARE @Time        VARCHAR(50)
DECLARE @Num        INT
DECLARE @Pos        INT
DECLARE @NextPos    INT
DECLARE @Name        VARCHAR(50)
DECLARE @TimeFormat    VARCHAR(50)
DECLARE @OrderId    VARCHAR(50)

    SELECT @Date = CONVERT(VARCHAR(50), GETDATE(),112)
    SELECT @Time = Convert(Varchar(50),GETDATE(), 114)

    SET @Num = 0
    SET @Pos = 1
    SET @TimeFormat = ''

    WHILE(@Pos <= LEN(@Time))
    BEGIN
        SELECT @NextPos = CHARINDEX(N':', @Time,  @Pos)
        IF (@NextPos = 0 OR @NextPos IS NULL)
            SELECT @NextPos = LEN(@Time) + 1
        SELECT @Name = RTRIM(LTRIM(SUBSTRING(@Time, @Pos, @NextPos - @Pos)))
        SELECT @Pos = @NextPos+1
       
        IF @TimeFormat = ''
            SET @TimeFormat = @Name
        ELSE
            SET @TimeFormat = @TimeFormat + @Name

        SET @Num = @Num + 1
    END

    SET @OrderId = @Date + @TimeFormat
    SELECT @OrderId = RTRIM(LTRIM(SUBSTRING(@OrderId, 1, len(@OrderId) - 3 )))

SELECT CONVERT(BIGINT, @OrderId) AS OrderId

END


12:27 PM



0 responses to "Generate Unique Id using DateTime"


Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

    Blog Archive

    • ▼  2010 (6)
      • ►  July (1)
      • ►  March (1)
      • ▼  February (3)
        • Generate Unique Id using DateTime
        • Resize Image at runtime
        • Search Procedure using ':' as seperator
      • ►  January (1)

    About Me

    My photo
    Prabir
    Web based application developer, working with Microsoft.Net technology.
    View my complete profile

    Search This Blog

    Labels

    • Blog Archive (1)
    • Image Resizing (1)
    • Search (1)
    • Stored Procedure (3)
    • XML (1)

    Followers

Copyright © All Rights Reserved. Prabir Kumar Basak's Blog | Converted into Blogger Templates by Theme Craft