I have the following table in SQL server.
It is called ScenarioData and I am using the data specifically the fieldValue column to append this data into a form. I have achieved this functionality but unfortunately, the form requires a start date that has to be the present-day or up to 30 days in the future. Because I am storing data in the database as soon as the date passes these values are redundant. I have a stored procedure that selects all values from this table based on the scenarioDataId.
I was thinking to ensure that the date is always viable I could add to this stored procedure to update the relevant rows (coverStartDateDay, coverStartDateMonth, coverStartDateYear) with the current date so that the value will always be accepted.
I have proceeded the following SQL
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = "CoverStartDateDay";
This I had hoped would append the current day to the rows in feilfValue wherein the column fieldName it equals the value coverStartDateDay. Unfortunately, I get an error saying CoverStartDateDay is not a column. Firstly where am I going wrong and secondly how can I achieve the functionality I desire?
Thanks in advance
Use single quote :
WHERE ScenarioData.FieldName = 'CoverStartDateDay';
In SQL Server double quotes are considers as column name. However, Double quotes have different usage depending on the setting QUOTED_IDENTIFIER.
By default QUOTED_IDENTIFIER is enabled (ON) & you can't use it to enclose literal strings.
For instance, if you want to go with double quotes then you need to disable QUOTED_IDENTIFIER.
SET QUOTED_IDENTIFIER OFF
Try this as varchar or string values should be passed in the single quote, not in the double quote.
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = 'CoverStartDateDay'
Double quotes are for identifiers. Somehow if you want to pass values which contain a single quote, then you can use '' i.e., two times a single quote not the double quote.
Related
I do have a piece of SQL code that gave me some problems. The pivot operation results in a table with a column name called 'INITIAL' which has the single quotes in the name. But the values in that column are integers, or NULLs. I managed to access the numbers in SQL developer by enclosing the column name with double quotes: "'INITIAL'". When copying the code to PowerBI, it is not accepted. I returns a
Expression.SyntaxError: Token Comma expected
at the location of the first use of the single quote.
Any idea how to fix this for powerBI?
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM("'INITIAL'")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST
that nearly did the trick. I misread your post and used this:
SUM(""'02_INITIAL'"")
And that works in PowerBI!
So in SQL Developer I need to use SUM("'INITIAL'")
Best is fo course to avoid quotes in column names, but since my Pivot is based on string values, I get them for free.
Many thanks for your suggestion that put me on the right track!
To escape a character you need to use double quotation ("") mark. Please try this:
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM(""'INITIAL'"")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST
I have a parameters #Year and #Month in my report, where month has assigned values like January(label): 1(values), February: 2, ...
My datased is feeded by stored procedure which has a filter
WHERE (cal.CalendarYear = #Year) AND (cal.MonthId = #Month)
When I check 'Allow multiply values' in parameter settings, it returns an error
Error converting data type nvarchar into int.
How can I select all values (by default)?
If you don't need to use a Stored Proc you can easily solve the problem.
Copy the SQL from your stored proc (excluding the PROC definition, just the main code) and paste it in as your dataset query.
Change your = #Year and = #Month to IN(#Year) and IN(#Month)
That's all there is to it, no joining, splitting or anything else.
SSRS will inject the parameter values as comma separated values correctly for you.
When you select multiple values, you pass the parameter to the procedure as an expression with Join().
Then in your procedure change your month parameter to be of type nvarchar(max).
Create a temp table and use a t-sql split string function (Google this, there are so many varieties but if you're stuck we can find one) to split the string into a table. Then inner join to your table in your procedure to filter on the selections.
Your error message about "nvarchar into int" suggests a data type mismatch between your SSRS parameter and your MonthId column. Check the Report Parameter Properties -> General -> Data type for the former and your table schema for the latter, and make sure they're either both text/varchar or both integers.
Allowing your query to handle multiple parameter values should be much simpler than needing to use joins and splits. Just use IN instead of =, and put your parameter name inside a set of brackets.
AND (cal.MonthId IN (#Month))
To set the defaults for your parameter, go to the Report Parameter Properties -> Default Values. Select the Specify values option, and add your numbers 1-12 as separate Value records.
EDIT:
I'm changing column datatype to Varchar, should suggestion work, answer will be upvoted
Full Story:
I receive data for a person with an associated temporary number for every person that is 5 digits long, I process this information and then send variables to a stored procedure that handles the inserting of this data. When sending the variables to the stored procedure I appear to be losing any prefixed 0's.
For example:
Number sent to stored Proc - Number actually inserted column
12345 - 12345
01234 - 1234
12340 - 12340
This only appears to be happening for numbers with a 0 in front. Meaning if I received:
00012 it would insert as 12
Is there a way where I could either update the column to always 0 pad to the left by a fixed number, meaning if we got 12 it would automatically make the value 00012.
OR
Is there a way to do this with the variable when its received by the stored procedure before the variable is inserted into the table.
Something along the lines of:
SET #zeroPaddedFixedNum = LeftPad(#numberRecieved, '0', 5);
Additionally, I now need to stop any more numbers from inserting and update all current incorrectly lengthed numbers. Any suggestions?
Perhaps it's just my Google ability that has failed but I have tried searching numerous pages.
For this, the column should be of varchar datatype. You can then do this
Insert into table(col)
select right('00000'+cast(#var as varchar(5)),5)
EDIT : To update existing data
Update table
set col=right('00000'+cast(col as varchar(5)),5)
where len(col)<5
As pointed out, you'll have to use VARCHAR(5) for your needs... But I would not change the columns type, if the values stored are numbers actually. Rather use one of the following, whenever you pass these values to your SP (You might use a computed column or a VIEW though).
Try
SELECT REPLACE(STR(YourNumber,5),' ','0');
The big advantage: In cases, where your number exceeds 5 digits, this would return *****. It is better to get an error than to get wrong numbers... Other approaches with RIGHT() might truncate your result unpredictably.
With SQL Server 2012 you should use FORMAT()
SELECT FORMAT(YourNumber,'00000')
I'm trying to convert a string to a decimal using this command:
SELECT cast(minimum_ticket_price AS DECIMAL(6,2)
FROM all_event_details
WHERE minimum_ticket_price ~ E'^\\d+$';
But this doesn't actually update anything in my database. It just displays the selected column in my terminal. Do I need to combine the select with an update? I've tried that but I must have the syntax wrong as I'm not able to get the conversion saved in the database.
Here's what I tried:
UPDATE all_event_details
SET minimum_ticket_price = cast(minimum_ticket_price AS DECIMAL(6,2))
WHERE ( minimum_ticket_price <> '') IS TRUE;;
Updating to a data type which minimum_ticket_price column's data can support is possible otherwise it will give an error.
for example if minimum_ticket_price column data type is varchar then your code must work.
What are you doing?
First add new column, decimal(but I'm goint to suggest to use the basics data type such real or double precision, are most efficient)
ALTER TABLE my_table ADD COLUMN minimum_ticket_priceR real ;
than
UPDATE all_event_details
SET minimum_ticket_priceR = to_number(coalesce(minimum_ticket_price,"0"),"999999D99") --for each row
than I'm going to suggest to drop the colum minimum_ticket_price and rename the other column (ever with ALTER TABLE):
What you did is not to understand, if minimum_ticket_price is string, you cannot set a number... and if is a number has no meaning to set it as string
On a SQLite database something I thought was very simple doesn't work at least under my conditions.
I have one column with names and some name contains apostrophe ('), which I want to remove. I know all names which contains an apostrophe, so I am not trying to query for apostrophes. I am doing something much simpler:
UPDATE table SET column_name="name surname1 surname2" WHERE column_name="name surname1'surname2";
which doesn't return what I expect. It doesn't produce an error but it doesn't modify any record.
SQL doesn't like reflexivity?
There should be no issue with querying the current value of a column to update the same column. Try escaping the single-quote by doubling it e.g. ''.
See: http://www.sqlite.org/lang_expr.html which reads:
A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal.
Therefore, your update should be:
UPDATE table SET column_name='name surname1 surname2' WHERE column_name='name surname1''surname2'
Update: Added explanation of escape mechanism.