![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
The STR function returns CHAR data type, but default 10 characters. You can use the 'length' argument of the function to specify 4: STR(DATEPART(yyyy, C.DataEntryDate), 4) Or simply cast to CHAR(4): CAST(DATEPART(yyyy, C.DataEntryDate) AS CHAR(4)) -- Plamen Ratchev http://www.SQLStudio.com |
#4
| |||
| |||
|
|
Hi all, I am trying to create a Card Number for clients, after inserting a new record, based on a clientID field and the year part of the date: CardNo = Year(Date) + PadL(ClientID, 7, '0') PADL is an UDF to left PAD a string. |
|
I wrote the following trigger, but I get an error when execute it: snip This trigger works if I use either of the 2 strings but not when I concatenate them as above. I get an error about truncating a binary or string variable and the insert is aborted but THE IDENTITY FIELD IS INCREMENTED NONTHELESS! Well it IS an "After Insert" trigger ... |
#5
| |||
| |||
|
|
Cypher wrote: Hi all, I am trying to create a Card Number for clients, after inserting a new record, based on a clientID field and the year part of the date: CardNo = Year(Date) + PadL(ClientID, 7, '0') PADL is an UDF to left PAD a string. What does the function look like? I wrote the following trigger, but I get an error when execute it: snip This trigger works if I use either of the 2 strings but not when I concatenate them as above. I get an error about truncating a binary or string variable and the insert is aborted but THE IDENTITY FIELD IS INCREMENTED NONTHELESS! Well it IS an "After Insert" trigger ... I used the following code to try and reproduce your problem but it ran without error. What are you doing differently? USE Test GO /****** Object: Trigger [dbo].[tr_makeCardNo] Script Date: 01/04/2010 19:07:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE ClientData ( ClientID int IDENTITY, CardNo varchar(50) NULL, DataEntryDate datetime NOt NULL) GO create function dbo.PADL(@str varchar(20),@final_length tinyint, @padwith char(1)) returns varchar(256) AS BEGIN return right(replicate(@padwith,@final_length) + @str, @final_length) END GO CREATE TRIGGER [dbo].[tr_makeCardNo] ON [dbo].[ClientData] AFTER INSERT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; UPDATE ClientData SET CardNo = STR(DATEPART(yyyy,C.DataEntryDate)) + dbo.PADL(C.ClientID,7,'0') FROM ClientData C JOIN Inserted I ON C.ClientID = I.ClientID END GO insert ClientData(DataEntryDate) values(getdate()); select * from ClientData -- HTH, Bob Barrows |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
This trigger works if I use either of the 2 strings but not when I concatenate them as above. I get an error about truncating a binary or string variable and the insert is aborted but THE IDENTITY FIELD IS INCREMENTED NONTHELESS! |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
Why did you store a computed column in a table? SQL is a declarative language. But you are using procedural code all over the place, in a TRIGGER and a UDF! You are still thinking as if you were writing for magnetic tape files or punch cards. You even use the word "record" because your mindset is all wrong. There is no such thing as an IDENTITY field. First of all, a column is nothing like a field. It is a proprietary, non-relational table property. Programmers who do not know RDBMS often use it to get a "record number" so that they can make their SQL look like a sequential file -- magnetic tape files or punch cards. The DDL skeleton would be something like this: CREATE TABLE Clients (client_id CHAR(7) NOT NULL PRIMARY KEY CHECK(client_id LIKE '<pattern>'), signup_date DATE DEFAULT CURRENT_DATE NOT NULL, (<build card number here>) AS card_nbr, etc); You need a way of getting a client identifier that can be validated and verified. IDENTITY has neither and it cannot even give you consecutive numbers. I would not expose the client id in the card number -- do you write your PIN on your bank card? You need to learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. |
#10
| |||
| |||
|
|
property. Programmers who do not know RDBMS often use it to get a "record number" so that they can make their SQL look like a sequential file -- magnetic tape files or punch cards. |
|
You need a way of getting a client identifier that can be validated and verified. IDENTITY has neither and it cannot even give you consecutive numbers. |
|
Why did you store a computed column in a table? SQL is a declarative language. But you are using procedural code all over the place, in a TRIGGER and a UDF! You are still thinking as if you were writing for magnetic tape files or punch cards. You even use the word "record" because your mindset is all wrong. There is no such thing as an IDENTITY field. First of all, a column is nothing like a field. It is a proprietary, non-relational table property. Programmers who do not know RDBMS often use it to get a "record number" so that they can make their SQL look like a sequential file -- magnetic tape files or punch cards. The DDL skeleton would be something like this: CREATE TABLE Clients (client_id CHAR(7) NOT NULL PRIMARY KEY CHECK(client_id LIKE '<pattern>'), signup_date DATE DEFAULT CURRENT_DATE NOT NULL, (<build card number here>) AS card_nbr, etc); You need a way of getting a client identifier that can be validated and verified. IDENTITY has neither and it cannot even give you consecutive numbers. I would not expose the client id in the card number -- do you write your PIN on your bank card? You need to learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. |
![]() |
| Thread Tools | |
| Display Modes | |
| |