Sometimes I store objects names in some of our databases, for example in some parameters table. Due I select records from this tables using ‘=’ or ‘LIKE’ comparison methods, I must take care to store this names always with or without brackets.
IF EXISTS (SELECT 1 FROM MYTABLE WHERE OBJ_NAME = '[OBJ_NAME]';
IF EXISTS (SELECT 1 FROM MYTABLE WHERE OBJ_NAME = 'OBJ_NAME';
However MS-SQL has some functions where you can use object names with or without brackets, for example OBJECT_ID() function. I’ve set up a minimal example on dbfiddle.uk.
CREATE TABLE TEST ( ID INT IDENTITY(1,1) PRIMARY KEY, OBJECT sysname NOT NULL ); GO INSERT INTO TEST VALUES ('[obj1]'),('obj2'),('obj3'),('[obj4]'); GO
Now I can use OBJECT_ID() to check if table TEST exists on this way:
IF OBJECT_ID('TEST') IS NOT NULL BEGIN SELECT 'TEST EXISTS.' OBJECT_ID; END GO | OBJECT_ID | | :----------- | | TEST EXISTS. | IF OBJECT_ID('[TEST]') IS NOT NULL BEGIN SELECT '[TEST] EXISTS.' OBJECT_ID; END GO | OBJECT_ID | | :------------- | | [TEST] EXISTS. |
It’ doesn’t matter if I pass TEST name with o without brackets, parser is smart enough to remove brackets.
Well, I can simulate it by adding an scalar function that remove brackets from one string:
CREATE FUNCTION UNQUOTENAME(@TXT NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) AS BEGIN RETURN IIF(LEFT(@TXT, 1) = N'[' AND RIGHT(@TXT, 1) = N']', SUBSTRING(@TXT, 2, LEN(@TXT) - 2), @TXT); END; GO
And then use it on this way:
SELECT dbo.UNQUOTENAME (N'[FIELD]') NAME1, N'FIELD' NAME2; GO NAME1 | NAME2 :---- | :---- FIELD | FIELD SELECT ID, OBJECT FROM TEST WHERE OBJECT LIKE 'obj%'; GO ID | OBJECT -: | :----- 2 | obj2 3 | obj3 SELECT ID, dbo.UNQUOTENAME(OBJECT) FROM TEST WHERE dbo.UNQUOTENAME(OBJECT) LIKE 'obj%'; GO ID | (No column name) -: | :--------------- 1 | obj1 2 | obj2 3 | obj3 4 | obj4
But my question is:
- Is there any hidden built-in function that removes brackets using T-SQL?
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!