SQLTeam.com | Weblogs | Forums

Help me! Ask about function on SQL

Hi everyone,

I need to build funciton SQL code follow this format: [date - A/B/QD-CC]
where: date is date
A is numercial order
B is year
QD is a policy or a decision
CC : code
How can I wrire a function for that format?

Can I try this:
CREATE function takecode
DECLARE
@ddmm Date
@STT INT
@Year INT
@AA varchar(50)
@BBB varchar(50)
Return data_tyoe as text
Begin
Return [ddmm-STT/YYYY/AA-BB]
End;

**Thank u for your concerning. I need to quickly anwser. Thank everyone
**

hi please dont mind my asking

why do you need a function

you can directly do SQL
select @ddmm-@STT/@YYYY/@AA-@BB

you can play around with this and get the result !!!!

Do you want to return a formatted string with that format - or is there some calculation that needs to be performed?

For a concatenation - just use CONCAT:

concat('[', replace(convert(char(5), @ddmm, 103), '/', ''), '-', @STT, '/', @Year, '/', @AA, '-', @BBB, ']')

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION dbo.takecode (
    @ddmm date,
    @STT int,
    @Year int,
    @AA varchar(50),
    @BBB varchar(50)
)
RETURNS varchar(200)
AS
BEGIN
RETURN (
    SELECT '[' + STUFF(CONVERT(varchar(5), @ddmm, 4), 3, 1, '') + '-' +
        CAST(@STT AS varchar(10)) + '/' + CAST(@Year AS varchar(5)) + '/' +
        @AA + '-' + @BBB + ']'
)
END /*FUNCTION*/

Yeah. Thank man. But my boss needs to setup function for that :slight_smile:

Thank man. You have a shorcut and easy structure. I noted that :slight_smile:

it's amasing. I am new mem of SQL. So you have me to understand detail of structure in SQL.Thank u :slight_smile: