Home > database >  Can statistics table one of the fields, the number of characters written to the same table of anothe
Can statistics table one of the fields, the number of characters written to the same table of anothe

Time:01-11

As title, need statistics article table content field the number of characters (not including the enter a newline, statistical character only with actual punctuation)
Write to the same table of zishu fields

CodePudding user response:

This kind of circumstance should use the computed columns, rather than repeatedly updating,
Because you never know when someone to insert or modify,

 USE tempdb for 
GO
IF OBJECT_ID (" article ") IS NOT NULL
DROP TABLE article
GO
The CREATE TABLE article (
[content] NVARCHAR (200)
)
GO
- note: insert have a carriage return and line feed
INSERT INTO article ([content]) VALUES (' ABC zhang 123
Li si '
);
-- -- -- -- -- -- -- -- above for testing table and test data -- -- -- -- -- -- -- -- --

- 1. Add computed columns
The ALTER TABLE article ADD zizhu AS LEN (REPLACE (REPLACE ([content], CHAR (10), ' '), the CHAR (13), "));
GO
- 2. Query
SELECT * FROM article


  • Related