How to store Spanish text in SQL server - sql

I need to store Spanish text in SQL server.
I am using varchar data type for string fields and collation is 'SQL_Latin1_General_CP1_CI_AS'.
Will it support for Spanish text?

Yes u could store in this way :
CREATE TABLE #TM(NAME NVARCHAR(MAX));
INSERT INTO #TM
VALUES(N'El hardware inalámbrico no autorizado se puede introducir fácilmente');
Explanation :
N actually stands for National language character set Which means that you are passing an NCHAR, NVARCHAR or NTEXT value
from Microsoft https://msdn.microsoft.com/en-IN/library/ms186939.aspx
Result :
NAME
El hardware inalámbrico no autorizado se puede introducir fácilmente

Related

SQL old data stuck and causing errors

So i have a little issue, i wrote this code :
CREATE TABLE Inscription (
ID_Societe VarChar(25) NOT NULL, #ID de la societe (auto generé)
Nom_Societe VarChar(15), #Nom de la societe (provided as an argument when calling inscription method)
PRIMARY KEY (ID_Societe)
);
INSERT INTO Inscription (ID_Societe, Nom_Societe)
VALUES ('S152', 'Renault');
CREATE TABLE Paiement (
ID_Paiement VarChar(25), #clé primaire (auto generated)
ID_Societe VarChar(25), # clé secondaire
Montant float, #montant a déduire du compte (provided as argument)
N_Carte VarChar(25), #provided as argument (4 4 numbers seperated by spaces)
Date_Exp Date, #provided as argument format:DD-MM-YYYY
Nom_Carte VarChar(25), #name of owner of card (not necessairly the buyer)
Date_Trans Date, #Date of transaction ( a java command that gives todays date then reformat ?)
Statut VarChar(25) #(Status is a keyword, but what is this ? state of bank account, state of card ? ask prof)
);
INSERT INTO Paiement (ID_Paiement, ID_Societe, Montant, N_Carte, Date_Exp, Nom_Carte, Date_Trans, Statut)
VALUES ('P1526', 'S152', 165.99, '5244 5698 1523 6948', 2020-12-10, 'Mouad ElKhabri', 2018-07-12,'i don\'t even know');
the problem i have is for the insert into, Date_Exp i used an old value of 11-12-1984, i messed up and used EU date format and it showed an error : "Data truncation: Incorrect date value: '1984' for column 'Date_Exp' at row 1"
even though i changed the value to insert, it still brings up that error
any ideas how to fix this ?
Your date literals need to be enclosed in single quotes. Otherwise it's just doing math and returning an integer to be stored in your date column (which is both not what you want and not what the database wants).
Try:
VALUES ('P1526', 'S152', 165.99, '5244 5698 1523 6948', '2020-12-10', 'Mouad ElKhabri', '2018-07-12','i don\'t even know');
If you want to be super explicit you can cast this date literal to a date as part of the insert like CAST('2020-12-10' AS DATE), but that's overkill. Just insert using ANSI standard date format with single quotes and you'll be good to go.
Also.. (I'm guessing here since you didn't share what database product you are working on and they differ pretty dramatically), you probably need to escape your single quote in
'i don\'t even know'
not with a backslash but with a second single quote like:
'i don''t even know'.

sql substr variable url extraction process

Context:
till now i uses to use regexp in sql to extract variable urls. I find it very slow and want to optimize it using substr and instr commands. That's important for me cause as i'm new in sql it serves me to be more familiar with such commands.
database:
my db is made by posts extracted from social platforms. text are called "titre". It contains variables url in different formats: www, http, https. I want to create a table or table view (i m not fixed) containing those url and the related id_post.
My work:
I have noticed that url always ends with a blank space, sthg like: "toto want to share with you this www.example.com in his post"
here stands what i ve done so far:
---longueur de la chaîne de caractère depuis https
select LENGTH(substr(titre, INSTR(titre,'https:'))) from post_categorised_pages where id_post = '280853248721200_697941320345722';
---longueur de la chaîne de caractère depuis le blanc
select LENGTH(substr(titre, INSTR(titre,' ', 171))) from post_categorised_pages where id_post = '280853248721200_697941320345722';
--- différence pour obtenir la longueur de chaîne de caractères de l'url
select LENGTH(substr(titre, INSTR(titre,'https:'))) - LENGTH(substr(titre, INSTR(titre,' ', 171))) as longueur_url from post_categorised_pages where id_post = '280853248721200_697941320345722';
---url
select substr(titre, 171, 54)from post_categorised_pages where id_post = '280853248721200_697941320345722';
Question:
How can i automotasize that over the whole table "post_categorised_page"?
Can i introduce case when statements to take into account https or http of www. and how can i do that?
Thanks a lot!!!!
Maybe, instead of the "HTTP", HTTPS" or "WWW" string you would need to have the name of a column.
In this case, probably, it would be helpful to have a definition table where to define all possible sources. This tabel to have 2 columns (ID and source_name).
Then, in your post_categorised_pages table, to insert also the source of the message (the ID value).
Then, into the query, to join with this definition table by ID and, instead of
select substr(titre, INSTR(titre,'https:'), (LENGTH(substr(titre, INSTR(titre,'https:'))) - LENGTH(substr(titre, INSTR(titre,' ', (INSTR(titre,'https:')))))))from post_categorised_pages where id_post = '280853248721200_697941320345722';
to have
select substr(titre, INSTR(titre,"definition table".source_name), (LENGTH(substr(titre, INSTR(titre,"definition table".source_name))) - LENGTH(substr(titre, INSTR(titre,' ', (INSTR(titre,"definition table".source_name)))))))from post_categorised_pages where id_post = '280853248721200_697941320345722';
Ok guys, here is the solution i have found (there is stil one mistake, see at end of post).
I use two views to finally extract my strings.
First view is create by a connect by request:
--- create intermediate table view with targeted pattern position
create or replace view Start_Position_Index as
with "post" as
(select id, text from "your_table" where id= 'xyz')
select id, instr(text,'#', 1, level) as position, text
from post
connect by level <= regexp_count(titre, '#');
then
--- create working table view with full references and blank position for each pattern match and string_lenght for each one
create or replace view _#_index as
select id, position as hashtag_pos, INSTR(text,' ', position) as blank_position, INSTR(text,' ', position) - position as string_length, text
from Start_Position_Index;
At the end you will be able to retrieve the hashtags (in that case) you were looking for in your string.
Ok so the mistakes:
- if the pattern you are looking for is at the end of your string it will retrieve a null value cause there will be no blank space (as it is at end of the string).
- it is not well optimized cause here i am working with views and not tables. I think using tables will be faster.
But i m pretty sure there is lots of things to do in order to optimize this code... any idea? The challenge were how to extract specific pattern recursively among strings whithout using costy regex and without using pl/sql stuff. What do you think of that?
How about using Oracle Full Text search?
This will index all the words from the column and will provide the hashtags or web addresses, as both are written in one word, without space in between.

Unit Conversion precision issue

Seeking an solution to the following problem.
In our application we were having an functionality of converting the units as per user selection. e.g . Convert kg to lbs or vice versa based on unit selection by user.
We have a screen where in user can select the unit in which he is providing the value e.g Kg or lbs
We have a table which contains all the formulas for the predefined unit which our system supports e.g 1Kg = 2.20462lbs like so
We fetch the formula from the database and calculate / evaluate the formula with actual values as provided by the user.
Since , user have ability to see any units as per his/her choice. we decided to save the value which user provided on screen in standard unit only in our database for easier maintainablity . in this case let's say we have decided to save in kg only database however , user may have selected 'lbs' on screen , but while saving we do convert the 'lbs' to 'kg' and then we will save to database
while reading from database we do convert to 'lbs' if user intended to see in 'lbs' on screen
Hope till now the implementaton is clear , now the problem to above solution is
Let's assume user have selected value '1' as 'lbs' and saved it
In database we will save in 'kg' so value of '1' is saved as 2.20462 in database
while reading again we will do convert to show in 'lbs' now we get value as 0.999998. this is expected due to precision in formula
But user asking to atleast show the same value what he as entered , in this case '1' but we have to do calculation to convert it back to 'lbs' since we are saving in 'kg' in database
I am seeking an solution to this problem what could be better solution with minimal changes.
Table Definition
Id | UnitConversion
1 | 100
2 | 200
3 | 30
Our application is developed on
Angularjs, webapi and sql server
Just for fun, here is a stripped down version of my conversion utility. Just showing MASS here. By storing the conversion factors as varchar, precision is at the individual record level.
Declare #Map table (MapType varchar(50),MapName varchar(50),MapValue varchar(50))
Insert Into #Map values
('Mass','tonnes (metric)','1000'),
('Mass','tons (US)' ,'907.18474'),
('Mass','tons (UK)' ,'1016.0469088'),
('Mass','stones' ,'6.35029318'),
('Mass','slugs(g-pounds)','14.593903'),
('Mass','Solar masses' ,'1.989e30'),
('Mass','pounds (troy)' ,'0.3732417216'),
('Mass','pounds' ,'0.45359237'),
('Mass','picograms' ,'1e-15'),
('Mass','ounces' ,'0.028349523'),
('Mass','ounces (troy)' ,'0.0311034768'),
('Mass','nanograms' ,'1e-12'),
('Mass','milligrams' ,'1e-6'),
('Mass','micrograms' ,'1e-9'),
('Mass','megatonnes' ,'1e9'),
('Mass','kilotonnes' ,'1e6'),
('Mass','kilograms' ,'1'), --- << Base
('Mass','hundredweights' ,'50.80234544'),
('Mass','hectograms' ,'0.1'),
('Mass','grams' ,'1e-3'),
('Mass','grains' ,'0.00006479891'),
('Mass','femtograms' ,'1e-18'),
('Mass','Earth masses' ,'5.980e24'),
('Mass','decagrams' ,'0.01'),
('Mass','cental' ,'45.359237'),
('Mass','carats (metric)','0.0002')
Declare #Value float = 1
Declare #From varchar(50)= 'kilograms'
Declare #To varchar(50)= 'pounds'
Select #Value * Max(IIF(MapName=#From,cast(MapValue as float),null)) / Max(IIF(MapName=#To,cast(MapValue as float),null))
From #Map
Where MapName in(#From,#To)
Returns
2.20462262184878
At Sql server side choose a finer scale then that you need to return to a client. For example, if you need 5 digits to the right of the decimal point, set scale to 7 for DB column
declare #n decimal(20,5) = 2.20462;
declare #t table (
m decimal(20,7)
);
insert #t(m) values
(1./#n),
(10./#n),
(1000./#n);
select cast(m*#n as decimal(20,5)) as r
from #t;
Note "Decimal and numeric are synonyms and can be used interchangeably." msdn.microsoft.com/en-us/library/ms187746.aspx

Send SQL Email (href with colum)

I want to link the word Click with URL in the email. This URL change for every row of SELECT.
I write this:
td = CAST('Click' AS XML), ''
But SQL server return:
Análisis de XML: línea 1, carácter 73; se esperaba punto y coma
Which translates to
XML parsing : line 1 , character 73; expected semicolon
The & symbol should be encoded as &. The parser sees &orig and expects a semicolon after it (or at least at some point in the string).

How to insert and display Chinese character in oracle database?

i have inserted Chinese character into oracle database. but the value is not suitable with the character, the value such as ¿¿ :¿¿¿!. the data type is varchar2. same problem when i want to display the data. How to store and display Chinese character into oracle 10g database ?
To support unicode character use driver Properties as shown :-
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#10.52.45.36:1521:ORCL";
Properties connectionProps = new Properties();
connectionProps.put("useUnicode","true"); //Property 1
connectionProps.put("characterEncoding","UTF-8" ); //Property 2
connectionProps.put("user", "SYSTEM");
connectionProps.put("password", "Root-123");
Connection conn = DriverManager.getConnection(url,connectionProps);
Statement stmt = conn.createStatement();
And ensure that the datatype in the oracle table is NVARCHAR/NCHAR. Also, while inserting use function such as TO_NCHAR('-some-unicode-value-') or N'-some-unicode-value-'
This should be it!