SQL server 2012: Make string fit Column Name limitations - sql-server

In my stored procedure I use string entered by the user in the application to create column names in a table. The string has no limitations in the application. Is there a way (function maybe) to convert the string so that it fits the Column Name limitations? I know that I can do REPLACE to remove HTML and special chars but I am looking for a solution that it will somehow guarantees that the converted string can be placed as Column Name.
I dont want to limit the user in the UI because the entered string can be used in other parts of the application where for example HTML chars are important
Example: String edited by the user: Cat's <b>name</b> . I wont to be converted to a valid column name string.

Related

Customize Normalization in SQL Server Full Text Search by replacing characters

I want to customize SQL Server FTS to handle language specific features better.
In many language like Persian and Arabic there are similar characters that in a proper search behavior they should consider as identical char like these groups:
['آ' , 'ا' , 'ء' , 'ا']
['ي' , 'ی' , 'ئ']
Currently my best solution is to store duplicate data in new column and replace these characters with a representative member and also normalize search term and perform search in the duplicated column.
Is there any way to tell SQL Server to treat any members of these groups as an identical character?
as far as i understand ,this would be used for suggestioning purposes so the being so accurate is not important. so
in farsi actually none of the character in list above doesn't share same meaning but we can say they do have a shared short form in some writing cases ('آ' != 'اِ' but they both can write as 'ا' )
SCENARIO 1 : THE INPUT TEXT IS IN COMPLETE FORM
imagine "محمّد" is a record in a table formatted (id int,text nvarchar(12))named as 'table'.
after removing special character we can use following command :
select * from [db].[dbo].[table] where text REPLACE(text,' ّ ','') = REPLACE(N'محمد',' ّ ','');
the result would be
SCENARIO 2: THE INPUT IS IN SHORT FORMAT
imagine "محمد" is a record in a table formatted (id int,text nvarchar(12))named as 'table'.
in this scenario we need to do some logical operation on text before we query in data base
for e.g. if "محمد" is input as we know and have a list of this special character ,it should be easily searched in query as :
select * from [db].[dbo].[table] where REPLACE(text,' ّ ','') = 'محمد';
note:
this solution is not exactly a best one because the input should not be affected in client side it, would be better if the sql server configure to handle this.
for people who doesn't understand farsi simply he wanna tell sql that َA =["B","C"] and a have same value these character in the list so :
when a "dad" word searched, if any word "dbd" or "dcd" exist return them too.
add:
some set of characters can have same meaning some of some times not ( ['ي','أ'] are same but ['آ','اِ'] not) so in we got first scenario :
select * from [db].[dbo].[table] where text like N'%هی[أي]ت' and text like N'هی[أي]ت%';

trim characters from string of one field and fill a new field for each record in SQL table

I added a new field "StrikePrice" to my existing table "OS1115".
I need to trim some characters from an existing field "symbol" and enter that into the new field.
I know I can do this:
UPDATE OS1115 SET StrikePrice = RIGHT(symbol, 4)
But the problem is that the amount of characters I need vary in length, but are always superseded by either a P or a C.
Here are a couple examples:
QQQ_112015C112.5
PCLN_112015P1287.5
NFLX_112015P107
I need to trim the numbers at the end of the string that come after the P or C and enter that in the new field.
so in this case that would result in:
112.5
1287.5
107
How can I do this?
I use MS SQL Express 2012
This would work:
RIGHT(symbol, PATINDEX('%[cp]%', REVERSE(symbol))-1)
though I'm sure there are a variety of ways to do it.
You got one answer while I was testing mine. Here's another way:
substring(StrikePrice,charindex('C',StrikePrice,6)+charindex('P',StrikePrice,6)+1,99)

Detecting split character in string

Recently I had a Problem with Splitting a HTML string into separate columns in Access 2010 and store date in SQL server 2008R2 table. This works more or less because the number of lines varies.
To avoid storing the HTML-tags I have a easier solution, I use a text field in Access which is Formated as Standard while the other text-field is formated as rich-text to contain the Format.
Inserting the same content, which is copied from a HTML mail in the Standard text field maintains the LINE FEED !
How can I "detect" which character is it to replace it by another character for further use??
Example:
<div><font face=Arial size=3 color=black>Customer Name</font></div>
<div><font face=Arial size=3 color=black>Customer Address</font></div>
<div><font face=Arial size=3 color=black>12345 City</font></div>
is the copied Content from html mail and inserted into the rich-text field.
If I copy this Content to a Standard text field it Looks like this:
Customer Name
Customer Address
12345 City
In the Server table the Content of the field Looks like this
Customer Name Customer Address 12345 City
But to maintain the line feed in the Access field there must be any character, but which??
Thanks!
Michael
In the SQL-Server table, multiple rows are shown as one row. If you set the ControlSource property of the Access TextBox to that column, it will be shown with multiple lines.
If you still want to know the characters: It's a combination of the ASCII control characters CR and LF (chr(13) & chr(10) or vbCrLf or vbNewLine). In SQL-Server syntax it's CHAR(13)+CHAR(10).

Why is Turkish Lira symbol ₺ replaced with ? in SQL server 2008 database

Any idea why the Turkish Lira symbol is replaced by a question mark when I insert it in a table in the database. See the image below
This is not a font issue. This is a Unicode (UTF-16) vs 8-bit Code Page character set issue (i.e. NVARCHAR vs VARCHAR). The character you are trying to use does not exist in the particular Code Page indicated by the default Collation of the DB in which you are executing this query. The Code Page used by the DB's default Collation is relevant here since your string literal is not prefixed with an upper-case "N". If it was, then the string would be interpreted as being Unicode and no conversion would take place. But since you are passing in a non-Unicode string, it will be forced into the current DB's default Collation's Code Page as the query is parsed. Any characters not available in that Code Page, and not having a Best-fit mapping, get turned into "?".
You can run the following to see for yourself:
SELECT '₺';
PRINT '₺';
It both prints AND displays in the results grid as ?
If you want to see what character SQL Server thinks it is, run the following:
SELECT ASCII('₺');
And it will return: 63
If you want to see what character has an ASCII value of 63, run this:
SELECT CHAR(63);
And it will return: ?
Now run this:
SELECT N'₺';
PRINT N'₺';
This will both print and display in the results grid correctly.
To see what character value the symbol really is, run the following:
SELECT UNICODE(N'₺'), UNICODE('₺');
This will return: 8378 and 63
But isn't 63 the question mark? Yes. That is because not prefixing the string literal '₺' with a capital "N" tells SQL Server that it is VARCHAR and so it gets translated to the default unknown character.
Now, if you were to execute this VARCHAR version in a DB that had a Collation tied to a Code Page that had this character, then it would work even when not prefixing the string literal with an upper-case "N". However, at the moment, I cannot find any Code Page used within SQL Server that supports this character. So, it might be a Unicode-only character, at least at far as SQL Server is concerned.
The way to fix this is:
Change the datatype of the field to NVARCHAR (I see in a comment on the question that the field is currently VARCHAR). If the field is VARCHAR then even if you use the N prefix on the string, the character will still get stored as ?, unless the Code Page specified by the Collation of the column supports this character, but again, I think this might be a Unicode-only character.
Change your INSERT statement to prefix the string field with a capital "N": (73, 4, N'(3) ₺'). Even if you change the field to NVARCHAR, if you don't prefix the string with N then SQL Server will translate the character to ? first and then insert the ?. This is because the query gets parsed before it gets executed, and parsing (for non-Unicode string literals and variables) is done in the Code Page of the DB's default Collation
Probably for the same reason my browser isn't displaying it in the title for this question: It isn't in the application's character set (or maybe not supported by the font).
In this case, my browser shows some numbers in a box (denoting the character code).
SQL-server is translating it to a known character instead.
Ensure you're storing it in a field that supports the character in it's character set (I think UTF-8 is sufficient)

blank spaces in sql server data

HI ALL,
I am using sql server express to store some data but it also store spaces with data. for example if a have a nchar(20) column in a table and i store "computer" (8 characters) to this column, then remaining character (20-8=12) is filled with blank spaces. Is there any way to over come this problem. Because when I shows this data to flow document (center alignment), then it produces alignment error.
Thanks for help
You can use the NVARCHAR data type instead. The NVARCHAR type is a variable length data type and will only store the actual data.
If you don't have control over the data types then you'll need to trim off any extra characters manually. In T-SQL you can do this with the RTRIM command.

Resources