TSQL, case, convert, replace end - sql-server

Need help understanding what I am doing with the syntax here, please help! I not only need to convert from float to decimal but I also need to override the original data being migrated to the same format and data (in this case) where necessary to match the newer data.
,CASE
WHEN fInvReqMargin IS NOT NULL THEN
(CONVERT(DECIMAL(7, 7), fInvReqMargin)(REPLACE(fInvReqMargin, fInvReqMargin, INVESTOR_REQUIRED_MARGIN_FC)))
ELSE NULL
END as INVESTOR_REQUIRED_MARGIN_FC
error: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Thank you!

The problem is with the CONVERT and REPLACE syntax. Without seeing the data I can't determine if you're trying to supply the style parameter to CONVERT. If you are then the syntax should be
CONVERT( DECIMAL ( 7,7 ), fInvReqMargin,(REPLACE(fInvReqMargin, fInvReqMargin, INVESTOR_REQUIRED_MARGIN_FC)))

Related

Msg 8152, Level 16, State 14, String or binary data would be truncate

I have procedure which put records to table, specifically from field data type money to field data type numeric(11,0) by
insert into tab1(decimal_field_1)
select convert(numeric(11, 0), money_field_1)
from tab2
After execution I get information with warning:
Msg 8152, Level 16, State 14
String or binary data would be truncated
I consciously convert that column from money to decimal(11,0) and I don't want to use set ansi_warnings OFF.
So what wrong I do? How correctly convert that data?
That error is typically involved in truncating strings (e.g., varchar, nvarchar) rather than numbers (e.g., the string is too long to fit).
If I have a too-large money value converting to numeric(11,0), it gives me the error Msg 8115, Level 16, State 8, Line 5 Arithmetic overflow error converting money to data type numeric.
As such, the error is unlikely to be triggered by the CONVERT, but instead in the INSERT. You could test this by just doing a select convert(numeric(11, 0), money_field_1) from tab2 which should work OK - showing the CONVERT is fine.
Are you only inserting into 1 column decimal_field_1?
If so, are you sure that decimal_field_1 is also numeric(11,0) rather than a varchar or nvarchar?
If you are inserting other columns, check the other fields. It's likely the error is coming from there.
The money data type has been deprecated for a while. Have a look at this article about avoiding it.
"MONEY is accurate between -922,337,203,685,477.5808 (-922,337
trillion) and 922,337,203,685,477.5807 (922,337 trillion)"
Try using DECIMAL(19, 4)
insert into tab1(decimal_field_1)
select convert(numeric(19, 4), money_field_1)
from tab2;

Incorrect encoding name syntax

Code:
declare #filedata xml
select #filedata = BulkColumn from openrowset(bulk 'E:\Scripts\apc.xml',single_blob) x;
the above is my code for which I got the error:
Msg 9442, Level 16, State 1, Line 2 XML parsing: line 1, character 38, incorrect encoding name syntax
could anyone help me with how I can resolve this? I can't understand where the syntax is wrong.
SQL-Server cannot import any kind of encoding...
You are importing this file as BLOB, this is just a bunch of bytes. This must be interpreted as a data type SQL Server knows.
You can either try to declare your variable as VARCHAR(MAX) or as NVARCHAR(MAX) and hope, that the content of this variable is what you expect.
In this case you can cut off the entire xml-declaration (<?xml blah?>) using CHARINDEX and SUBSTRING or STUFF. Alternatively you can use REPLACE to set a different value to your encoding="blah". Use UTF-8 if VARCHAR worked for you, or UTF-16 in case of NVARCHAR.
If this does not help, you must change the file on disc before you read it. Best is utf-16, which can be read into XML directly. But don't forget to write this into the encoding or to strip off the declaration.

Convert date from 1/26/2016 14:54 format to 20160126 in MSSQL

Having date in 1/26/2016 14:54 format and need it in 20160126 format in MSSQL.
I am using REPLACE(CONVERT(DATE, DATE_OF_AQUISITION,112),'-','')
but it is giving error
"Msg 9807, Level 16, State 0, Line 47 The input character string does
not follow style 112, either change the input character string or use
a different style."
Simple Example But I'm not sure
Just remove 112 conversion you have added
declare #dt VARCHAR(20) = '1/26/2016 14:54'
select REPLACE(CONVERT(DATE,#dt),'-','')
Convert to date before replace.
SELECT REPLACE(CONVERT(CHAR(10),CONVERT(DATE,DATE_OF_AQUISITION), 112),'/','')
This is example which show a result that you want.
Hope this is also help for you.
select Convert(varchar,TaskDate,112) TaskDate from XYZ.
Result-20161121

ms sql server executes 'then' before 'when' in case

when i try to select
select
case when (isnumeric(SUBSTRING([VZWECK2],1,9)) = 1)
then CONVERT(decimal,SUBSTRING([VZWECK2],1,9))
else null
end as [NUM]
from table
sql-server gives me:
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to numeric.
[VZWECK2] is a char(27). is this a known bug? because it seems to me it executes the convert before it does the case, which defies the purpose of my select. i know that there are values that are not numeric obviously, which is why i need the case statement to weed them out.
for some reason selecting
select
case when (isnumeric(SUBSTRING([VZWECK2],1,9)) = 1)
then 99
else null
end as [NUM]
from table
yields no errors and behaves as expected
The problem is that ISNUMERIC is very forgiving, and that ISNUMERIC returns 1 is unfortunately no guarantee that CONVERT will work. This is why SQL Server 2012 and later introduced TRY_CAST and TRY_CONVERT.
If you are converting whole numbers, a more reliable check is to make sure the string consists of only digits with NOT LIKE '%[^0-9]%' (that is, it must not contain a non-digit anywhere). This is too restrictive for some formats (like floating point) but for integers it works nicely.
Do you know the value which throws the error? IsNumeric is not exactly fool-proof, for example:
select ISNUMERIC('$')
select ISNUMERIC('+')
select ISNUMERIC('-')
all yield 1
Alternatively, you could go with TRY_PARSE instead.
Edit: TRY_PARSE is introduced in sql server 2012, so may not be available to you.

Query giving error

The below query is giving error, please help me.
declare #dateScorecard datetime
set #dateScorecard = convert(varchar, 4)+'/1/'+ convert(varchar,2013)
select #dateScorecard
select top 1 i_CurrentMonthColor from AK_ScoreCardDetails scr
where datediff(m, convert(Datetime, convert(varchar, 4)+'/1/'+ convert(varchar,2013)), #dateScorecard)
error details:
Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Two quick comments:
First, you don't need to use convert on those numbers--just put them right into your query, i.e.
set #dateScoreCard = convert('4/1/2013');
Also, in the 'where' clause, you are not comparing the result of the datediff to anything There should be a section after the datediff clause, i.e.:
where datediff(.....) > 2
for example. Your datediff function only gives you a number that is the difference of the two dates you gave it, in the terms you define, which is months here. Use that number to compare to some value you want to make your sql statement work for you.

Resources