How can I insert a value into a column with power? Please see the below example:
Can it be done via the UNISTR function?
insert into table values ('2332239 12'); -- I intentionally want to insert the number into a varchar field.
If you mean you want to insert a string that ends with Unicode superscript 12 you can just put Unicode characters in a Unicode string and insert them into a Unicode column:
INSERT INTO table VALUES(N'123¹²')
(Your column will have to be an NVARCHAR)
If your column is a varchar and you can't change it you'll have to encode the data somehow, and decode it very time you want to use it (not ideal)
Related
I have a table (RPT.table1) that contains data that was exported from an ArcGIS Online application. One of the columns in the table (GlobalID) was exported as a nvarchar(255) datatype. I need to convert this column to a uniqueidentifier datatype before I insert this data into another table (CFAdmin.table2).
The values in the GlobalID column were once unique identifiers and already contain hyphens (B4A6AA96-42DF-48D9-A3E0-4C7F88ED3E1D).
I've tried using
ALTER TABLE RPT.table1
ALTER COLUMN GlobalID uniqueidentifier;
and in an INSERT INTO statement
INSERT INTO CFAdmin.table2 (GlobalID)
SELECT CAST(GlobalID AS uniqueidentifier)
FROM RPT.table1;
For both methods I get an error
Msg 8169, Level 16, State 2, Line 1
Conversion failed when converting from a character string to uniqueidentifier.
I'm relatively new to SQL Server and am guessing there is a pretty simple solution.
Thanks for reading.
Chances are there's an issue with your data, not with your code/syntax. There are probably values in the source table that are invalid as unique identifiers. I would investigate by just looking at the data in your source table and trying to find the values that would cause the error. See what this query returns:
Select GlobalID from RPT.table1 WHERE GlobalID NOT LIKE '________-____-____-____-____________'
(Those underscores blur together, so for the sake of being explicit: 8 underscores, dash, 4 underscores, dash, 4 underscores, dash, 4 underscores, dash, 12 underscores)
And maybe check
Select GlobalID from RPT.table1 WHERE GlobalID IS NULL
I tried to add a row to my table named Clienti, I opened query tool and wrote this query, anyway it isn't working, can you tell me the reason? It says the array isn't correctly defined.
INSERT INTO "Clienti"(
"Nome", "Cognome")
VALUES ('example', 'example2');
The problem is that your fields are defined as arrays of strings, not strings. That's why PostgreSQL is complaining about your INSERT statement: You're trying to put a single string into a field that's defined as an array of strings.
"Nome" character(20)[]
means Nome is an array of strings of 20 characters.
You probably want
"Nome" varchar(20)
(for a string of up to 20 characters)
or
"Nome" text
(for no length limit).
See the PostgreSQL documentation for more information about character types.
Try this insert query it will work.
INSERT INTO Clienti(Nome, Cognome)
VALUES ('example', 'example2');
I have a table with a column configured to hold nvarchar data type.
I am trying to add a row using
INSERT INTO TABLE_NAME VALUES (value1, value2...)
Sql-server gets stuck on a 180 character string that I am trying to assign to the nvarchar data type column returning:
Error: The identifier that starts with [part of string] is too long.
Maximum length is 128.
I don't understand why this is happening since nvarchar(max) should hold 2GByte of storage as I read here: What is the maximum characters for the NVARCHAR(MAX)?
Any ideas of what I've got wrong here?
UPDATE:
The table was created with this:
CREATE TABLE MED_DATA (
MED_DATA_ID INT
,ORDER_ID INT
,GUID NVARCHAR
,INPUT_TXT NVARCHAR
,STATUS_CDE CHAR
,CRTE_DTM DATETIME
,MOD_AT_DTM DATETIME
,CHG_IN_REC_IND CHAR
,PRIMARY KEY (MED_DATA_ID)
)
And my actual INSERT statement is as follows:
INSERT INTO MED_DATA
VALUES (
5
,12
,"8fd9924"
,"{'firstName':'Foo','lastName':'Bar','guid':'8fd9924','weightChanged':false,'gender':'Male','heightFeet':9,'heightInches':9,'weightPounds':999}"
,"PENDING"
,"2017-09-02 00:00:00.000"
,"2017-09-02 00:00:00.000"
,NULL
)
By default, double quotes in T-SQL do not delimit a string. They delimit an identifier. So you cannot use double quotes here. You could change the default but shouldn't.
If this is being directly written in a query window, use single quotes for strings and then double up quotes within the string to escape them:
INSERT INTO MED_DATA VALUES (5, 12, '8fd9924', '{''firstName'':''Foo'',''lastName'':''Bar'',''guid'':''8fd9924'',''weightChanged'':false,''gender'':''Male'',''heightFeet'':9,''heightInches'':9,''weightPounds'':999}', 'PENDING', '2017-09-02T00:00:00.000', '2017-09-02T00:00:00.000', NULL)
But if, instead, you're passing this string across from another program, it's time to learn how to use parameterized queries. That'll also allow you to pass the dates across as dates and not rely on string parsing to reconstruct them correctly.
Also, as noted, you need to fix your table definitions because they've currently nvarchar which means the same as nvarchar(1).
Are you aware of what an Identifier is? Here is a hint - it is a NAME. SQL Server is not complaining about your data, it is complaining about a field or table name. SOmehow your SQL must be totally borked so that part of the text is parsed as name of a field or table. And yes, those are limited to 128 characters.
This is clear in the error message:
Error: The identifier
clearly states it is an identifier issue.
How can store both text and integer in same field in sql?
Want to store like this: ex: Jh_123
Am using phpmyadmin.
And also want to use as "UNIQUE" field?
Thanks
You could use NVARCHAR data type to keep text and numbers.
Character data types that are either fixed-length, nchar, or
variable-length, nvarchar, Unicode data and use the UNICODE UCS-2
character set.
As in your comment, would be like:
CREATE TABLE Test
(
Id INT,
Name NVARCHAR(50) UNIQUE
)
INSERT INTO Test VALUES (1, 'Jh_123')
SELECT * FROM Test
And result will be: 1 Jh_123
use nvarchar(max) then retrieve data by putting where condition
when u want to get only int values then you can use
where column>0
I have a column in a table with a varchar datatype. It has 15 digits after the decimal point. Now I am having a hard time converting it to a numeric format.. float, double etc.
Does anyone have any suggestions?
Example :
Table1
Column1
-------------------
-28.851540616246499
-22.857142857142858
-26.923076923076923
76.19047619047619
I tried using the following statements and it doesn't seem to work :
update table1
set Column1 = Convert(float,column1)..
Any suggestions ?
You can use the decimal data type and specify the precision to state how many digits are after the decimal point. So you could use decimal(28,20) for example, which would hold 28 digits with 20 of them after the decimal point.
Here's a SQL Fiddle, showing your data in decimal format.
Fiddle sample:
create table Table1(MyValues varchar(100))
insert into Table1(MyValues)
values
('-28.851540616246499'),
('-22.857142857142858'),
('-26.923076923076923'),
('76.19047619047619')
So the values are held as varchar in this table, but you can cast it to decimal as long as they are all valid values, like so:
select cast(MyValues as decimal(28,20)) as DecimalValues
from table1
Your Sample
Looking at your sample update statement, you wouldn't be able to convert the values from varchar to a numeric type and insert them back in to the same column, as the column is of type varchar. You would be better off adding a new column with a numeric data type and updating that.
So if you had 2 columns:
create table Table1(MyValues varchar(100), DecimalValues decimal(28,20))
You could do the below to update the numeric column with the nvarchar values that have been cast to decimal:
update Table1
set DecimalValues = cast(MyValues as decimal(28,20))
I think you're trying to actually change the data type of that column?
If that is the case you want to ALTER the table and change the column type over to float, like so:
alter table table1
alter column column1 float
See fiddle: http://sqlfiddle.com/#!6/637e6/1/0
You would use CONVERT if you're changing the text values to numbers for temporary use within a query (not to actually permanently change the data).