Converting Raw(16) to GUID - sql

Is there a way to create an SQL script that will convert a list of Oracle RAW(16) to GUID?
I need to find a simple and fast way to convert about 14 million items. I have exported the list to several delimited files and I am able to import this data into a schema.

A RAW(16) basically is a GUID: it's a 16-byte hex value. So one option is to just leave it alone. Oracle will implicitly cast between character and hex, so if you're looking for a row whose raw value is FE2BF132638011E3A647F0DEF1FEB9E8, you can use a string in your query:
SELECT *
FROM myTable
WHERE myRaw16Column = 'FE2BF132638011E3A647F0DEF1FEB9E8';
If you want to change the RAW(16) to CHAR(32) for your conversion you can use RAWTOHEX as #tbone suggests.
INSERT INTO NewTable (myGUIDColumn, ...)
SELECT RAWTOHEX(myRawColumn), ...
FROM OldTable
If you want to make it a CHAR(36) dash-formatted GUID, things get complicated quickly:
INSERT INTO NewTable (myGUIDColumn, ...)
SELECT REGEXP_REPLACE(myRaw16Column, '(.{8})(.{4})(.{4})(.{4})(.*)', '\1-\2-\3-\4-\5'), ...
FROM OldTable

Related

How to do explicit conversion of FLOAT to a VARCHAR field in SQL

I'm getting a query from a column float (with no precision) and inserting it in another table and comumn float (with no precision as well) but I'm getting this error:
Error (265) Insufficient result space for explicit conversion of FLOAT value '3.8833137793643' to a VARCHAR field.
The query:
INSERT INTO TableA
SELECT DISTINCT max(price_1) AS PriceValue
FROM TableB
This does may or may not answer your question. But the query should be written as:
INSERT INTO TableA (<column name>)
SELECT MAX(price_1) AS PriceValue
FROM TableB;
Notes:
An aggregation query with no GROUP BY returns exactly one row. SELECT DISTINCT is not necessary.
You should include the columns being inserted.
Your problem is clearly that the column is not wide enough. If you have defined the table as:
create table tableA (
col varchar
);
Then you have not specified a length and are depending on the default in the database. Do you even know what the default is? In this case, it is 1. And that is not long enough for your value. You just need to provide a long-enough length:
create table tableA (
col varchar(255)
);
All that said. I strongly discourage you from storing number values as strings. That is likely to create problems now and in the future. Use the appropriate types for your data.

Reading Unicode strings from SQL Server

I know strings need to be prefixed with N' in SQL Server (2012) INSERT statements to store them as UNICODE but do they have to be retrieved (SELECT statement) in a certain way as well so they are in UNICODE?
I am able to store international strings correctly with N notation but when I run SELECT query to fetch the records back, it comes as question marks. My query is very simple.
SELECT COLUMN1, COLUMN2 FROM TABLE1
I am looking at other possible reasons that may have caused this issue but at least I want to eliminate the SQL statement above. Should it read COLUMN1 and COLUMN2 columns correctly when they both store UNICODE strings using N notation? Do I have to do anything to the statement to tell it they are UNICODE?
Within management studio you should not need to do anything special to display the correct values. Make sure that the columns in your table is defined as Unicode strings NVARCHAR instead of ANSI strings VARCHAR.
The following example demonstrates the concept:
CREATE TABLE UnicodeExample
(
MyUnicodeColumn NVARCHAR(100)
,MYANSIColumn VARCHAR(100)
)
INSERT INTO UnicodeExample
(
MyUnicodeColumn
,MYANSIColumn
)
VALUES
(
N'איש'
,N'איש'
)
SELECT *
FROM UnicodeExample
DROP TABLE UnicodeExample
In the above example the column MyUnicodeColumn is defined as an NVARCHAR(100) and MYANSIColumn is defined as a VARCHAR(100). The query will correctly return the result for MyUnicodeColumn but will return ??? for MYANSIColum.

store both text and number in sql

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

Converting varchar to nvarchar in SQL Server failed

I have SQL Server table that contains columns of type varchar(50) as a result of a CSV import using the SQL Server Import wizard.
I was wanting to know how I can change this data type to nvarchar(9) without getting a SQL Server truncation error.
I tried doing a bulk update to set the data types and column sizes that I need but still had the truncation error message when I tried to load the csv into the empty database table I created (with my required data types that I need).
Grateful for any help.
Since you are willing to lose data and nvarchar will only be able to store 9 non-unicode charaters, then select only 9 characters from your source table, You do the truncation rather than Sql server doing it for you.
The Following Query will trim any White spaces from the strings, Then take only 9 characters from the string and convert them to NVARCHAR(9) for you.....
CREATE TABLE New_TABLE (Col1 NVARCHAR(9), Col2 NVARCHAR(9))
GO
INSERT INTO New_TABLE (Col1, Col2)
SELECT CONVERT(NVARCHAR(9),LEFT(LTRIM(Col1), 9))
,CONVERT(NVARCHAR(9),LEFT(LTRIM(Col2), 9))
FROM Existing_Table
GO
Bulk insert into temp table with varchar(50) and insert to actual table
insert into tableName
select cast(tempcolumn as nvarchar(9)) from temptable
And it is also important to check field types of destination table. Just spent 3 hours because of same error with random casting, trimming, substring and at the end noticed, that colleague created table with too short field lengths.
I hope it helps somebody...
If you encounter this error during Import/Export Tasks, you can use the select cast(xxx as nvarchar(yyy)) as someName in the "Write a query to specify the data to transfer" option
varchar and nvarchar only use the length needed for the data stored. If you need unicode support certainly convert to nvarchar, but modifying it from 50 to 9 - what is the point?
If your data is ALWAYS exactly 9, consider using char(9), and following one of the transformation suggestions above...

Conversion failed when converting the varchar value '1,' to data type int

I have table categories (c) and an another table (x) with a column which can contain cat IDs separated with comma as varchar data type. I want to Select related categories but I'm having error "Conversion failed when converting the varchar value '5,' to data type int." when trying to select:
SELECT ID, Title FROM c WHERE ID IN (SELECT catIDs FROM x WHERE ID=any);
The subquery returns data like "1,3,4"
You need to split the 1,3,4 string returned by the subquery into separate int values. SQL Server does not have a built-in function to do it, but you can use this user-defined function.
Create the function dbo.Split in your database and then re-write your query as follows:
SELECT ID, Title
FROM c
WHERE ID IN
(
SELECT s
FROM dbo.Split(',', '1,3,4')
)
I replaced the subquery with example results 1,3,4 to shorten the query and make it easier to understand.
If I get it right, you actually have values like "1,3,4" in your column catIDs. So you should extract a substring in the select of your subquery.
By the way, I'm not an MS SQL Server expert, but it's probably a bad database design to do so. I'm not sure the RDBMS engine will use indexes in such a case...