I have a procedure that accepts string as input in format dd/mm/yyyy but I need to call this procedure from another application and I am forced to give the format yyyy-mm-dd.
So I have extracted and changed the string to dd/mm/yyyy but the procedure is throwing incorrect syntax error.
Required format: dd/mm/yyyy e.g '11/04/2018'
Input format: yyyy-mm-dd e.g. '2018-04-11'
So my procedure would be
Required format is abcdprocedure('11/04/2018')
My formula
abcdprocedure(SUBSTRING('2018-04-11',9,2)+'/'+SUBSTRING('2018-04-11',6,2)+'/'+SUBSTRING('2018-04-11',1,4))
Error: Incorrect syntax near ''2018-04-11''
I am unable to understand the problem.
Edit
Procedure is being called in below way:
select * from abcdprocedure('11/04/2018')
When you execute a stored procedure within T-SQL, you may supply a value, a variable or DEFAULT for each parameter. You may not supply an arbitrary expression.
Move your expression out into a separate line that places the result into a variable and use that when calling the stored procedure.
(Also, seriously, please reconsider your use of strings here. T-SQL has perfectly good datetime related data types that are designed to hold datetimes. You only have formatting issues because you're working with strings)
EDIT
Procdure is being called in below way:
select * from abcdprocedure('11/04/2018')
Um, no. If that line of code works, then what we're talking about is not what T-SQL calls a stored procedure. Stored procedures are standalone blocks of code and cannot be integrated into larger queries. Again, if this works, please identify what abcdprocedure actually is (a table-valued function?) and update your question.
Documentation says -
A table-valued function returns a single rowset (unlike stored
procedures, which can return multiple result shapes). Because the
return type of a table-valued function is Table, you can use a
table-valued function anywhere in SQL that you can use a table. You
can also treat the table-valued function just as you would a table.
First of all you should check the function 'abcdprocedure'
You can store your conversion from yyyy-mm-dd to dd/mm/yyyy in a variable
Declare #v_date date;
#v_date=SELECT CONCAT( SUBSTRING('2018-04-11',9,2),'/',SUBSTRING('2018-04-11',6,2),'/',SUBSTRING('2018-04-11',1,4));
And then you can call your table valued funcyion
select * from abcdprocedure(#v_date)
you can use below query for this
SELECT CONCAT( SUBSTRING('2018-04-11',9,2),'/',SUBSTRING('2018-04-11',6,2),'/',SUBSTRING('2018-04-11',1,4));
It will work in mysql and sql server both.
OR
SELECT (SUBSTRING('2018-04-11',9,2)+'/'+SUBSTRING('2018-04-11',6,2)+'/'+SUBSTRING('2018-04-11',1,4))
**OUTPUT:**
11/04/2018
Related
I am trying to create a stored procedure, which receives a string and places it in the WHERE clause after processing it.
I created the function and when I call it from the body of a SQL statement, it returns the correct values:
('J1245',j3456','j1098')
However, when I call the function inside the WHERE clause as shown below, SQL does not show any records.
(Altcode in dbo.myfunction(#codes))
When I hard code it inside the WHERE clause, the SQL statement shows records.
(Altcode IN ('J1245',j3456','j1098'))
I suspect the function is returning a string (VARCHAR). If we reference the function anywhere in a SQL statement, the value returned by the function serves as a scalar value. The contents of the value don't change that. Any punctuation, single quotes, backticks, parens, commas, keywords, identifiers, et al. that happen to appear in the string are just part of the value.
The parser sees the return from the function simply as a single value, not as part of the SQL text.
It's not possible (in a single statement) to make the value returned by a function "become" part of the SQL text to be parsed.
As an example, in the WHERE clause, if we write:
WHERE altcode IN ( myfunc() )
The function myfunc is going to be evaluated, and the return will be single value, of a certain datatype, for example maybe a VARCHAR. Any parens or commas within the value are not interpreted as part of the SQL text. Those are just characters that are within the value.
It's as if we wrote SQL like this:
WHERE altcode IN ( ? )
And supplied a single value in place of the question mark placeholder. The SQL parser isn't seeing a list of values, it's not seeing any SQL to be executed. All the statement is seeing is a value.
It matters not one whit that we might supply a value that looks like SQL text, for example:
WHERE altcode IN ( '(SELECT code FROM all_altcodes)' )
That would be equivalent to writing
WHERE altcode = '(SELECT code FROM all_altcodes)'
And that is going to look for an exact match of altcode to the string literal.
Seems like there's a single quote missing in the value returned by the function, but maybe that's a typo in the question.
To get the string value returned by the function included as part of the SQL text, we would need to use dynamic SQL.
We would have to first call the function, and return the string. And then do some string concatenation to come up with another string that contains the SQL statement we want to execute, and then execute that string as a SQL statement.
So that would be two separate statement executions... one to get the function evaluated; and a second statement that is dynamically constructed, as a string, incorporating the value returned by the function.
(The question is tagged "MySQL", but I suspect this question is actually regarding SQL Server (given the reference to dbo. ?)
Preparing and executing dynamic SQL is similar in MySQL and SQL Server, but there are differences in the syntax.
MySql has a function CONCAT_WS that I use to export multiple fields with a delimiter into a single field. Works great!
There are multiple fields being stored in a database I query off of that has data that I need to extract each field individually but within each field the data need to include a delimiter. I can most certainly do a concatenate but that does take awhile to set-up if my data requires up to 100 unique values. Below is an example of what I am talking about
Stored Data 01020304050607
End Result 01,02,03,04,05,06,07
Stored Data 01101213
End Result 01,10,12,13
Is there a function in MySQL that does the above?
I am not that familiar with mysql but I have seen questions like this come up before where a regular expression function would be useful. There are user-defined functions available that allow Oracle-like regular expression functions to be used as their support is weak in mysql. See here: https://github.com/hholzgra/mysql-udf-regexp
So you could do something like this:
select trim(TRAILING ',' FROM regexp_replace(your_column, '(.{2})', '\1,') )
from your_table;
This adds a comma every 2 character then chops off the last one. Maybe this will give you some ideas.
I'm just starting developing reports in SSRS and would appreciate some help with this issue if possible! I'm selecting a dataset from a Dynamics database and want to then pass them to a SQL Server stored procedure referenced in another dataset to retrieve data from another database. I have created a report parameter and set it to Allow multiple values and to retrieve its values from a query and set it to the field that I want to retrieve.
The dataset would look like this:
U1234
U5678
U6789
In the dataset that uses the stored procedure I have set up a parameter, #pnum, and in the Parameter Value field I have created an expression using the Join statement like this:
Join(Parameters!pnum.Value, ", ")
When this gets passed to the stored proc it seems to be passing a string formatted like this:
'U1234, U5678, U6789'
Whereas what I would like to achieve is this:
'U1234', 'U5678', 'U6789'
so that I can use them in an IN statement. Is there a way of doing this within SSRS?
Many Thanks!
To anyone else experiencing this issue, the assumption made in the question on how the values are passed to the stored procedure and how they can be used are incorrect.
The value passed from the join expression would be formatted as such, without single quotes at the start and end:
U1234, U5678, U6789
Further to this, when passed to a stored procedure as a single string this can only be used as an in list by using dynamic SQL.
To parse out and filter on the passed values, the string will need to be split on the delimiter and inserted into a table (temporary or otherwise) to be joined to.
A suitable splitting can be found here (though others exist that may better suit your needs) utilising logic as follows:
declare #xml as xml,#str as varchar(100),#delimiter as varchar(10)
set #str='A,B,C,D,E'
set #delimiter =','
set #xml = cast(('<X>'+replace(#str,#delimiter ,'</X><X>')+'</X>') as xml)
select N.value('.', 'varchar(10)') as value from #xml.nodes('X') as T(N)
If you don't have to pass the values to a stored procedure and are using hardcoded datasets (Shared or not) you can actually directly use the parameter value without additional processing either in the query or by adding a join expression to the parameter value in the report:
select cols
from tables
where cols in(#MultiValueParameterName)
You have to add an extra field with the value wrapped in quotes.
Like this:
SELECT field1 AS display, '''' + field1 + '''' AS value
In my stored procedure we have to input the 2013-12-12 to get data. Now with a new system we only want to use month and year to get the correct data. We use Microsoft SQL server 2008.
This is how we get data today:
exec dbo.Month '2013-12-12'
This is how we want the input to be to get data:
exec dbo.Month '2013-12'
-- #dateStr format yyyy-mm-dd
Possible to change?
I don't really know where in the database to start looking, and the stored procedure is to big to start adding here I guess.
There is no type for only a datapart of the date. You can of course change the input parameter to varchar() of some size and then do the necessary format inside the SP with 'Convert' (http://msdn.microsoft.com/en-us/library/ms187928.aspx). But it is not really that nice a solution.
I have a need to use the OUTPUT parameter for providing the input to the stored procedure. I tested and found that we can use OUTPUT as INPUT as well.
Is there any limitations / exceptions for this case?
Yes. Number of parameters is 2100. Otherwise no.