I would need to edit and change the content for around 2000 field values in my database "alyssa" for "products" table and for all its field of "productId".
Every productId is in numeric format "1234567890".
I would need to change all of these numeric id's to conform the following format:
"alyssa-PROD-1234567890-1".
So, e.g. if the product id was like "3298374237" it would need to be changed to the format of "alyssa-PROD-3298374237-1".
The fixed values are "alyssa-PROD-" and tailing "-1". These won't be changing.
Please could you possibly kindly assist me to form a script / command which I could run in order to batch change all of the productId's to the described format?
Thank you so much in advance! :) Yours, Alyssa
You can use update:
update t
set productId = concat('alyssa-PROD-', productId, '-1');
Note: This assumes that productId is stored as a string, albeit a string consisting only of digits. If you only want numeric strings to be converted:
update t
set productId = concat('alyssa-PROD-', productId, '-1')
where productId regexp '^[0-9]+$';
Related
Initial situation
I have a relatively large table (ca. 0.7 Mio records) where an nvarchar field "MediaID" contains largely media IDs in proper hexadecimal notation (as they should).
Within my "sequential" query (each query depends on the output of the query before, this is all in pure T-SQL) I have to convert these hexadecimal values into decimal bigint values in order to do further calculations and filtering on these calculated values for the subsequent queries.
--> So far, no problem. The "sequential" query works fine.
Problem
Unfortunately, some of these Media IDs do contain non-hex characters - most probably because there was some typing errors by the people which have added them or through import errors from the previous business system.
Because of these non-hex chars, the whole query fails (of course) because the conversion hits an error.
For my current purpose, such rows must be skipped/ignored as they are clearly wrong and cannot be used (there are no medias / data carriers in use with the current business system which can have non-hex character IDs).
Manual editing of the data is not an option as there are too many errors and it is not clear with what the data must be replaced.
Challenge
To create a query which only returns records which have valid hex values within the media ID field.
(Unfortunately, my SQL skills are not enough to create the above query. Your help is highly appreciated.)
The relevant section of the larger query looks like this (xxxx is where your help comes in :-))
select
pureMediaID
, mediaID
, CUSTOMERID
,CONTRACT_CUSTOMERID
from
(
select concat('0x', Replace(Ltrim(Replace(mediaID, '0', ' ')), ' ', '0')) AS pureMediaID
--, CUSTOMERID
, *
from M_T_CONTRACT_CUSTOMERS
where mediaID is not null
and mediaID like '0%'
and xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
) as inner1
EDIT: As per request I have added here some good and some bad data:
Good:
4335463357
4335459809
1426427996
4335463509
4335515039
4335465134
4427370396
4335415661
4427369036
4335419089
004BB03433
004e7cf9c6
00BD23133
00EE13D8C1
00CCB5522C
00C46522C
00dbbe3433
Bad:
4564589+
AB6B8BFC.8
7B498DFCnm
DB218DFChb
d<tgfh8CFC
CB9E8AFCzj
B458DFCjhl
rytzju8DFC
BFCtdsjshj
DB9888FCgf
9BC08CFCyx
EB198DFCzj
4B628CFChj
7B2B8DFCgg
After I did upgrade the compatibility level of the SQL instance to SQL2016 (it was below 2012 before) I could use try_convert with same syntax as the original convert function as donPablo has pointed out. With that the query could run fully through and every MediaID which is not a correct hex value gets nicely converted into a null value - really, really nice.
Exactly what I needed.
Unfortunately, the solution of ALICE... didn't work out for me as this was also (strangely) returning records which had the "+" character within them.
Edit: The added comment of Alice... where you create a calculated field like this:
CASE WHEN "KEY" LIKE '%[^0-9A-F]%' THEN 0 ELSE 1 end as xyz
and then filter in the next query like this:
where xyz = 1
works also with SQL Instances with compatibility level < SQL 2012.
Great addition for people which still have to work with older SQL instances.
An option (although not ideal in terms of performance) is to check the characters in the MediaID through a case statement and regular expression
Hexadecimals cannot contain characters other than A-F and numbers between 0 and 9
CASE WHEN MediaID LIKE '%[0-9A-F]%' THEN 1 ELSE 0 END
I would recommend writing a function that can be used to evaluate MediaID first and checks if it is hexadecimal and then running the query for conversion
currently I'm facing a problem where I select the data from db where it returns the data into 2 tables.
I have attached a screenshot of the output. Hope you guys can help. Thanks
This is Screenshot.
You need to format the output in the SQL*Plus to make the output looks proper.
You can use
COLUMN <column_name> FORMAT <format>;
for string:
COLUMN SCENE_NAME FORMAT a15
Example for numbers:
COLUMN SCENE_ID FORMAT 99
For more details on column formatting, please refer Oracle documentation here
Also, you will need to set the LINESIZE using
set linesize 250
here, 250 means the total character that can fit into one line and the size of the character is calculated based on the column format.
Before that you mention which database table you will display. After do select query
I am simply trying to sort by my acquisition date column that has dates in this type of format: '04/02/2019' etc... I have created and column named ACQ_DATE_CONVERTED in an new table and I usual get the results such as: 2019-04-02. Instead I am getting the
error Message 241 (Conversion failed when converting date and/or time from character string)
I have tried the following:
SELECT [ITEM], [ACQ_DATE],
CONVERT(DATE,[ACQ_DATE]) AS ACQ_DATE_CONVERTED
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
After my results I then order by ACQ_Date_Converted.
SELECT [ITEM], [ACQ_DATE]
FROM [ABC].[dbo].[My_Store_records_CONVERTED]
ORDER by [ACQ_DATE_CONVERTED]
My expected results should look something like this in the table:
Column_A Column_B ColumnC
Rows ITEM ACQ_DATE ACQ_DATE_CONVERTED
1. ITEM_1 04/09/2007 2007-04-09
2. Store item 01/26/2008 2008-01-26
etc...
Can you please try the following query:
REPLACE(CONVERT(VARCHAR(10), [ACQ_DATE], 111), '/', '-') AS ACQ_DATE_CONVERTED
Since you are converting from ACQ_DATE, so you can use ORDER BY [ACQ_DATE], it will work.
Demo on DB Fiddle
Thank you everyone that helped with input for this issue.
I discovered that I had a hidden character in all of the cells for the column I wanted to ORDER by; therefore, causing every line to fail. However, with the below I was able to overcome this stumbling block. I hope that anyone having this issue will find my solution below helpful.
REMOVING THE HIDDEN CHARACTER.
, REPLACE([ACQ_DATE], CHAR(10),'') AS [ACQ_DATE_CONVERTED]
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
and sorting the new table by ACQ_DATE_Converted;
ALTER TABLE [ABC].[dbo].[My_Store_records_CONVERTED] ALTER COLUMN [ACQ_DATE_CONVERTED] DATE NULL;
and then of course;
ORDER by [ACQ_DATE_CONVERTED]
Best Regards,
Dex
So I'm working on editing some SQL code and I've just began learning it. I'm trying to fix an update query so it updates a table's value5 column with a corresponding database value. The value type from the database is a number, which I want to convert to a date and place into my table. The database number is in yyyymmdd format so I've been trying to use datefromparts() which doesn't work. Anyone have any ideas?
UPDATE tbl INNER JOIN dB ON
(dB.value1= tbl.value1 OR
dB.value2 =tbl.value2 ) AND
(LEFT(dB.value3 ,5)=tbl.value3 ) AND
(dB.value4 =tbl.value4 )
SET tbl.value5 = DateFromParts(Left(dB.value5,4),Mid(dB.value5,5,2),Right(dB.value5,2))
WHERE tblInvoice.value5 IS NULL;
The current program uses the code
"SET tbl.value5 = dB.value5"
instead (it runs perfectly fine) and I am having another issue with testing the conversion SQL code (datefromparts()). Because I am converting from numbers to time/date, I have to go into the design view of the target table and change the input data type of the value5 column from numbers to time/date. When I run the query with the conversion SQL code, the query stalls for a bit and no values get updated, leaving me with just a blank value5 column. If I now want to fill in the original number values, I change the SQL code back into its original "SET tbl.value5 = dB.value5", change the input data type from time/date to numbers, and rerun the program. The query stalls and no values are updated, and I am again left with blank columns, even though the same code left me with the corrected update values before the modifications to the SQL and table input Data types. I come from a VBA background and I'm just really confused with how this is working. Any tips would be appreciated, thanks!
Have you tried with substring instead?
SELECT DATEFROMPARTS ( left('20101231',4), substring('20101231',5,2), right('20101231',2) ) AS Result;
MS Access (and MS Jet too) have no DateFromParts function. Using DateSerial instead.
SET tbl.value5 = DateSerial(Left(dB.value5, 4), Mid(dB.value5, 5, 2), Right(dB.value5, 2))
It's not clear if you work with T-SQL or Access SQL. In Access, you can use Format:
SET tbl.value5 = CDate(Format(dB.value5, "####\/##\/##"))
In T-SQL you could use a similar method.
I wrote a basic update query:
Update WA SET WA.Time_Updated = Replace(Time_Updated, 'PM', ' ');
to which I don't get any real error message other than
Microsoft can't update 251 records etc due to type conversion error
There are 5000 records in there. I have the date column as Date/Time and all my other columns (non-dates) as Short Text. The query just does not update anything in the table and keeps it previously was. Any ideas?
Just convert your text times to Date values:
Select *, TimeValue([Time_Updated]) As TimeUpdated From WA
Then, when you display TimeUpdate, format the value as you like.
Can deal with the imported structure.
Consider:
Hour("12:03:00 PM") + Minute("12:03:00 PM")/60 + Second("12:03:00 PM")/3600
This calculates to 12.05
So don't change the raw data, calculate in query. Just use your field name in place of the static value in the expression.