I have 4 field in my table :
NO. | Specification | Specification Value | Description
1 Color Blue a
1 Size 6" a
2 Color Red b
2 Size 8" b
I am trying to Pivot it and its giving me an error.
TRANSFORM FIRST(Specification Value)
SELECT items.'No.'
FROM items
GROUP BY items.'No.'
PIVOT Specification
The error is : "Syntax error(missing operator) in query expression 'FIRST(Specification Value)'."
Please help.
Thank you
Remove the space in the Specification Value. The First() function takes one expression or argument. You are passing it two, so it thinks you are passing an expression with two arguments and have forgotten the operator.
Related
There is the table column which holds the comma-separated values, e.g:
abc321,rd512,spwewr
I need to extract the substring which starts from the user-defined pattern.
Example:
Input Pattern | Expected result
abc abc321
r rd512
spwe spwewr
b NULL
Following fails in Druid SQL:
SELECT SUBSTRING('abc321,rd512,spwewr', POSITION('r' IN 'abc321,rd512,spwewr'), 2)
This is the known Druid bug:
" Substring operator converter does not handle non-constant literals correctly":
https://issues.apache.org/jira/browse/CALCITE-2226
I think the way to go is to use REGEXP_EXTRACT() or REGEXP_LIKE()
but I cannot figure out the specific syntax.
select regexp_extract('abc321,rd512,spwewr', 'rd[^,]+', 0)
I've narrowed down a data issue on a legacy SQL Server 2008 database.
The column is a 'float'. SSMS shows four of the records as '0.04445' but when i query for all records that match the first value, only 3 of the four are returned. The last record is somehow different, i suspect it is off by 0.0000000001 or something and the SMSS GUI is rounding it for display(?). Using the '<' operator has similar results ('where my_column < 0.04445' returns three of the four) This is causing some catastrophic calculation errors in the calling app.
I tried casting it to a decimal ('SELECT CAST(my_column as DECIMAL(38,20)) FROM...') but all four records just come back 0.044450000000000000000000000000
I suspect that there are many other similar errors in this same column, as the data has been entered in various ways over the years.
Is there any way to see this column in its full value/precision/mantissa, rather than the rounded value?
I can't change the schema or the app.
Update - using the 'Edit Top 200 Rows' feature, I can see that about three quarters of them are 0.044449999999999996 and the other quarter are ecxactly 0.04445. But I can't get it to display that level of accuracy in a regular query result
You can use CONVERT(VARBINARY(8), my_column) to the number in its original form. What you get should be 0x3FA6C226809D4952 or 0x3FA6C226809D4951. And what number that really is? 3FA6C226809D4951 is binary
0 01111111010 0110110000100010011010000000100111010100100101010001
0 => number is positive
01111111010 => 1018-1023 = -5 is exponent (so we get 2^-5)
1.0110110000100010011010000000100111010100100101010001 => 6405920109971793*2^-52
so the 0x3FA6C226809D4951 is exactly 6405920109971793*2^-57, which is 0.044449999999999996458388551445750636048614978790283203125
and 0x3FA6C226809D4952 is exactly 6405920109971794*2^-57, which is 0.04445000000000000339728245535297901369631290435791015625
So, your question is really about SSMS, not about your application or SQL Server itself, right? You want to see the actual float values in SSMS without rounding, right?
By design SSMS rounds float during display. For example, see this answer.
But, you can see the actual value that is stored in the column if you convert it to a string explicitly using CONVERT function.
float and real styles
For a float or real expression, style can have
one of the values shown in the following table. Other values are
processed as 0.
0 (default) A maximum of 6 digits. Use in scientific notation, when appropriate.
1 Always 8 digits. Always use in scientific notation.
2 Always 16 digits. Always use in scientific notation.
3 Always 17 digits. Use for lossless conversion.
With this style, every distinct float or real value is guaranteed to
convert to a distinct character string.
It looks like style 3 is just what you need:
convert(varchar(30), my_column, 3)
Here is my test:
DECLARE #v1 float = 0.044449999999999996e0;
DECLARE #v2 float = 0.044445e0;
SELECT #v1, #v2, convert(varchar(30), #v1, 3), convert(varchar(30), #v2, 3)
Result that I see in SSMS:
+------------------+------------------+-------------------------+-------------------------+
| (No column name) | (No column name) | (No column name) | (No column name) |
+------------------+------------------+-------------------------+-------------------------+
| 0.04445 | 0.044445 | 4.4449999999999996e-002 | 4.4444999999999998e-002 |
+------------------+------------------+-------------------------+-------------------------+
I have a columns with list of fruit names. I am trying to extract the right most value from the cell. Cell can have one value or n values. In which case, I am expecting to pull the last value.
Given below sample shows the actual value (col name: name_of_fruits) and the expected value (col name: expected_value )
name_of_fruits, expected_value
apples|oranges, oranges
apples|bananas, bananas
apples, apples
apples|mango|pears, pears
I tried performing the below code:
select name_of_fruits, right((name_of_fruits),position('|' in reverse(name_of_fruits))-1) as expected_value from table
I get an error SQL Error Invalid operation : Invalid length (context : Negative value given)
Could anyone assist to pull the required values. Thanks
I am using Redshift DB.
Try using REGEXP_REPLACE:
SELECT
name_of_fruits,
REGEXP_REPLACE(name_of_fruits, '.*|', '') AS expected_value
FROM yourTable;
The pattern .*| works nicely here, because in the case of a name string which does have more than one fruit, it would strip everything off leaving only the final fruit. And, in the case of a name string having only one name, the replacement would just no-op, also leaving the same fruit name.
I try to divide 2 ReportItems FreeNumberField_14 and FreeNumberField_13 with the following expression:
=(ReportItems!FreeNumberField_14.Value / ReportItems!FreeNumberField_13.Value)
When running the report I receive the following error:
The Value expression for the textrun
‘Textbox82.Paragraphs[0].TextRuns[0]’ contains an error: Input string
was not in a correct format. (rsRuntimeErrorInExpression)
What is the correct way to dividing 2 ReportItems?
=Cdec(ReportItems!freefield1.Value)/Cdec(ReportItems!freefiled2.Value)
try to Convert it to Int,Decimal Or Number before you divide like this
=(CDec(ReportItems!FreeNumberField_14.Value) / CInt(ReportItems!FreeNumberField_13.Value))
I am using SQL Server 2012. In the column PROJECT_NAME I have line items, all with the same format, that look like this:
PROJECT_NAME
--------------
Caulk, Norman v BPI
Caulk, Norman v BWD
Carper, Robert v ECH
I am trying to extract the first name (second name in the text string) and am using this query:
select
substring(Project_name,(charindex(',',PROJECT_NAME,0)),((CHARINDEX(' v ',PROJECT_NAME)-
(charindex(',',PROJECT_NAME)))))
from RPT_PROJ_MAIN pm
When I run this query I get the following error:
Invalid length parameter passed to the LEFT or SUBSTRING function.
I have isolated all of the different expressions and they all work fine on their own. If I replace the minus operator with a + then the query runs fine and I cannot figure out why?
That means that despite you saying that all rows have the same format some of them don't.
Specifically if a value has no v <something> at the end you'll get exactly that error, because the third parameter to SUBSTRING() function will have a negative value
Here is SQLFiddle demo
Hi I have solution for this just used below mentioned trick to handle this issue. Require to Declare value you want to minus. Just see the example below.
Declare #Val numeric=2
PRINT LEFT('My Name is Bhumesh Shah',charindex('is','My Name is Bhumesh Shah'))
PRINT LEFT('My Name is Bhumesh Shah',charindex('is','My Name is Bhumesh Shah')-#val)
Output:
My Name i
My Name