How to replace a value in Bracket with negative value in ssis - sql

I have imported data into a database from an excel file and some of the columns contain values are like (392.03), (2.25), (65.00). Actually these values should be -ve values can you guys help me to convert these into -392.03,-2.25,-65.00

DECLARE #NumValue DEC (9,3) = 0
DECLARE #StrValue varchar(15) = '(392.03)'
IF LEFT(#StrValue,1)='(' and RIGHT(#StrValue,1)=')'
BEGIN
SET #StrValue = SUBSTRING(#StrValue,2,LEN(#StrValue)-2)
SET #NumValue = convert(DEC (9,3), #StrValue)
END
SELECT #NumValue

Try this in the derived column
SUBSTRING([Paid Amount],1,1) == "(" ? REPLACE(REPLACE([Paid Amount],"(","-"),")","") : [Paid Amount]
Then use a Data conversion step to convert the value yo Float DT_R4

Related

How to update a table concatenating the strings in two columns?

I want to update a column setting it up as the concatenation of the string in one column + the file extension (in this case, '.AVI').
I tried the following but it affects 0 rows; I know for a fact that there are records which have a 'FALSE' value for Video_Name.
Any ideas what I'm doing wrong? Thanks!
declare #creativity varchar
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name=concat(#creativity,'.AVI') where Video_Name = 'false' and
#creativity=Creativity
I would expect something like this:
declare #creativity varchar(255); -- length is really important
set #creativity = <some value goes here> ; -- value is really important
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name = concat(#creativity, '.AVI')
where Video_Name = 'false' and
Creativity = #creativity;
Or perhaps you just want:
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name = concat(creativity, '.AVI')
where Video_Name = 'false' ;

How can I modify this SQL SELECT statement to change non-numeric values?

The dataset I am using can be found here.
I am using SSIS to upload the dataset into MS SQL Server.
I uploaded everything as text, and am trying to create a working table with proper data types by inserting values from the raw table.
CREATE TABLE [WRK_demographics]
(
[RowNumber] INT IDENTITY(1,1)
,[DBN] VARCHAR(10)
,[Name] VARCHAR(1000)
,[schoolyear] VARCHAR(100)
,[fl_percent] FLOAT
,[frl_percent] FLOAT
,[total_enrollment] INT
,[grade9] INT
,[grade10] INT
,[grade11] INT
,[grade12] INT
,[ell_num] INT
,[ell_percent] FLOAT
,[sped_num] INT
,[sped_percent] FLOAT
,[ctt_num] INT
,[selfcontained_num] INT
,[asian_num] INT
,[asian_per] FLOAT
,[black_num] INT
,[black_per] FLOAT
,[hispanic_num] INT
,[hispanic_per] FLOAT
,[white_num] INT
,[white_per] FLOAT
,[male_num] INT
,[male_per] FLOAT
,[female_num] INT
,[female_per] FLOAT
)
INSERT INTO [WRK_demographics]
(
[DBN]
,[Name]
,[schoolyear]
,[fl_percent]
,[frl_percent]
,[total_enrollment]
,[grade9]
,[grade10]
,[grade11]
,[grade12]
,[ell_num]
,[ell_percent]
,[sped_num]
,[sped_percent]
,[ctt_num]
,[selfcontained_num]
,[asian_num]
,[asian_per]
,[black_num]
,[black_per]
,[hispanic_num]
,[hispanic_per]
,[white_num]
,[white_per]
,[male_num]
,[male_per]
,[female_num]
,[female_per]
)
SELECT
[DBN]
,[Name]
,[schoolyear]
,[fl_percent]
,[frl_percent]
,[total_enrollment]
,[grade9]
,[grade10]
,[grade11]
,[grade12]
,[ell_num]
,[ell_percent]
,[sped_num]
,[sped_percent]
,[ctt_num]
,[selfcontained_num]
,[asian_num]
,[asian_per]
,[black_num]
,[black_per]
,[hispanic_num]
,[hispanic_per]
,[white_num]
,[white_per]
,[male_num]
,[male_per]
,[female_num]
,[female_per]
FROM [RAW_demographics_20170706]
However, the issue I am having is that for cells where there is no value, there is text instead of a null cell. So, implicit conversion cannot convert the data to int/float. Is there a way I can modify the SELECT statement to update the values of non-numeric cells to NULL? If I didn't have so many columns I would update each one in the raw table like this before inserting into the working table:
UPDATE [RAW_demographics_20170706]
SET [fl_percent] = NULL
WHERE ISNUMERIC([fl_percent]) <> 1
I'm wondering if there is a more efficient path to take.
You'd want to use a case statement since your error should be coming from empty strings not an actual NULL
SELECT
[DBN]
,[Name]
,[schoolyear]
,case when [fl_percent] = '' then 0.0 else fl_percent end
,case when [frl_percent] = '' then 0.0 else flr_percent end
...
...
,case when [grade12] = '' then 0 else grade12 end
...
FROM [RAW_demographics_20170706]
I also wouldn't use ISNUMERIC for this situation. It will cause problems as it returns true for an array of cases other than an int or float.
These return true, but would fail for conversions:
select isnumeric('$')
select isnumeric('1e4')
etc...
That's what TRY_CAST is for: "Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null."

How can I structure an IF statement inside of a SELECT statement?

I'm hoping that what I have paints a clear enough picture of what I am trying to accomplish:
SELECT [Date]
,[ChargeCode]
,[ChargeDescription]
,[HHY_Qty]
,[PatPrice]
, IF ISNUMERIC(HHY_Qty) AND ISNUMERIC(PatPrice)
BEGIN
CAST(HHY_Qty AS INT) * CAST(PatPrice AS INT) AS ExtAmt
END
ELSE
0 AS ExtAmt
END
FROM [dbo].[ChargeDetails]
WHERE PatientNumber = '1271'
HHY_Qty and PatPrice are both VARCHAR types in a MSSQL database. They were created with a BULK INSERT from a very very very dirty CSV from an AS400 export. Here, I am trying to do some multiplication IF the fields are numeric values, otherwise ExtAmt should be 0. Is that possible? If not,, is there a workaround?
Use a CASE statement:
CASE WHEN ISNUMERIC(HHY_Qty) = 1 AND ISNUMERIC(PatPrice) = 1 THEN CAST(HHY_Qty AS INT) * CAST(PatPrice AS INT) ELSE 0 END AS ExtAmt

MS-SQL - Extracting numerical portion of a string

I have an MS-SQL table, with a column titled 'ImportCount'.
Data in this column follows the below format:
ImportCount
[Schedules] 1376 schedule items imported from location H:\FOLDERA\AA\XX...
[Schedules] 10201 schedule items imported from location H:\FOLDERZZ\PERS\YY...
[Schedules] 999 schedule items imported from location R:\PERS\FOLDERA\AA\XX...
[Schedules] 21 schedule items imported from location H:\FOLDERA\MM\2014ZZ...
What I would like to do is extract that numerical portion of the data (which varies in length), but am struggling to get the right result. Would appreciate any help on this!
Thanks.
Try
select left(ImportCount, patindex('%[^0-9]%', ImportCount+'.') - 1)
select SUBSTRING(ImportCount,13,patindex('% schedule items%',ImportCount)-13) from table name
Try this..You can declare it as a SQL function also.
DECLARE #intText INT
DECLARE #textAplhaNumeric varchar(100)
set #textAplhaNumeric = '1376 schedule items imported from location'
SET #intText = PATINDEX('%[^0-9]%', #textAplhaNumeric)
BEGIN
WHILE #intText > 0
BEGIN
SET #textAplhaNumeric = STUFF(#textAplhaNumeric, #intText, 1, '' )
SET #intText = PATINDEX('%[^0-9]%', #textAplhaNumeric)
END
END
Select #textAplhaNumeric //output is 1376
It will work in case of NULL or empty values.
Please try:
SELECT LEFT(Val,PATINDEX('%[^0-9]%', Val+'a')-1) from(
SELECT
STUFF(ImportCount, 1, PATINDEX('%[0-9]%', ImportCount)-1, '') Val
FROM YourTable
)x

Conversion from nvarchar to float isn't working

I am unable to get the following query to work due to errors upon conversion from nvarchar to float; I need to convert theData field to float in order to round it, but the data is originally an NVARCHAR(20) because the column holds character data as well. I've tried casting each of the instances of theData to float but it still didn't work, can anyone tell me what I'm missing?
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Round(Convert(float, [theData]),(Len([theData])-Charindex('.',[theData])))
ELSE Null END
WHERE tblData.theFlag =1;
I have tried the following two variants...
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Round(Convert(float, [theData]),(Len(Convert(float, [theData]))-
Charindex('.',Convert(float, [theData]))))
ELSE Null END
WHERE tblData.theFlag =1;
and...
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Convert(nvarchar(20),Round(Convert(float, [theData]),(Len(Convert(float,
[theData]))- Charindex('.',Convert(float, [theData]))))) ELSE Null END
WHERE tblData.theFlag =1;
Can't answer the question because we don't know the error you're getting but... you should move the case to where... it'll make it more readable.
UPDATE tblData
SET tblData.theNumericData = Convert(float, [theData])
WHERE IsNumeric([theData]) = 1 AND tblData.theFlag =1;