Solved

Need help converting simple function in SQL Server from Oracle...

Posted on 2009-05-14
5
233 Views
Last Modified: 2012-05-07
I am an Oracle PL/SQL guy but have hit an issue in trying to get this function converted to SQL Server.

The TO_CHAR piece of this doesn't exist (among other things).

Can someone please help me convert this?

Thanks in advance!

B
CREATE OR REPLACE FUNCTION format_sequence (p_number IN INTEGER)
    RETURN VARCHAR2
IS
    v_num   INTEGER := FLOOR (p_number / 1000);
BEGIN
    RETURN    CHR (FLOOR (v_num / (26 * 26)) + 65)
           || CHR (FLOOR (MOD (v_num, 26 * 26) / 26) + 65)
           || CHR (MOD (v_num, 26) + 65)
           || TO_CHAR (MOD (p_number, 1000), 'fm000');
END;

Open in new window

0
Comment
Question by:cyimxtck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 24388236
The function is here:
USE [Expres]
GO
/****** Object:  UserDefinedFunction [dbo].[format_sequence]    Script Date: 05/14/2009 20:53:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[format_sequence] 
(
	-- Add the parameters for the function here
	@p_number int
)
RETURNS varchar(6)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @ResultVar varchar(6)
	DECLARE @v_num int
	
	SET @v_num = FLOOR (@p_number / 1000)
 
    SET @ResultVar = CHAR (FLOOR (@v_num / (26 * 26)) + 65)
           + CHAR (FLOOR ((@v_num % (26 * 26)) / 26) + 65)
           + CHAR (@v_num % 26 + 65)
           + REPLACE(STR (@p_number % 1000, 3, 0),' ','0')
 
	-- Return the result of the function
	RETURN @ResultVar
 
END

Open in new window

0
 

Author Comment

by:cyimxtck
ID: 24388389
PERFECT and exactly what I needed.

Thank you so much!!!!
0
 

Author Closing Comment

by:cyimxtck
ID: 31581597
Perfect in every way!
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24388419
The
USE [Expres]
GO
at the beginning should be replaced by your database name.

If you'll modify this function, you have to change
CREATE FUNCTION
to
ALTER FUNCTION
0
 

Author Comment

by:cyimxtck
ID: 24388431
I have already done that (so different than Oracle) but thank you so much!

B
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question