In Azure Stream Analytics query how do you concatenate strings together? - azure-stream-analytics

I'm attempting to modify one of my data values by concatenating a string to a date.
Below is what I've tried for my query but there seems to be some problem with my syntax.
Can someone point out what I'm doing wrong?

Try casting to a string first:
CONCAT(
CAST(created as nvarchar(max)),
' something else to concatenate'
)

If you hover over the query it will tell you what's wrong with the syntax.
Anyway, the problem it is likely to be that the created column is not a text format so it needs to be converted to a VARCHAR before being used by the CONCAT function. Try changing it to:
SELECT
CONCAT(CAST(created AS NVARCHAR(MAX)), ', XXX-YOUR-TEXT-XXX') AS NewName
[...]
Plenty of query examples here:
https://azure.microsoft.com/en-gb/documentation/articles/stream-analytics-stream-analytics-query-patterns/

Related

How can I automatically extract content from a field in a SQL query?

The environment I am currently working in is Snowflake.
As a matter of data sensitivty, I will be using pseudonyms for my following question.
I have a specific field in one of my tables called FIELD_1. The data in this field is structured as such:
I am trying to figure out how to automatically extract from my FIELD_1 the output I have in FIELD_2.
Does anyone have any idea what kind of query I would need to achieve this? Any help would be GREATLYappreciated! I am really quite stuck on this problem.
Thank you!
You seem to want everything up to the first four numbers. Then to replace the underscores with spaces. If so:
select replace(regexp_substr(field_1, '^[^0-9]*[0-9]{4}'), '_', ' ')
Or alternatively, if you want the first three components separated by underscores:
select replace(regexp_substr(field_1, '^[^_]+_[^_]+_[0-9]{4}'), '_', ' ')
If the data is as simplistic in reality as you've described here, you can use a variable-length LEFT() function in conjunction with REPLACE() to get the desired output:
SELECT FIELD_1, REPLACE(LEFT(FIELD_1, LEN(FIELD_1)-10),'_',' ') AS FIELD_2
FROM table_name
See also:
SELECT - Snowflake Documentation
LEFT - Snowflake Documentation
REPLACE - Snowflake Documentation
LENGTH, LEN - Snowflake Documentation

SQL Server 2016: How to read different substrings from a text with special characters

I have a string with the value 'Initiator;abcd#gmail.com'.
I would like to read these two fields in two separate queries.
';' would be the delimiter always.
I used the below query and it works for fetching the email. However it looks overcomplicated to me.
select SUBSTRING(NOTES (CHARINDEX(';',NOTES,1)+1),LEN(NOTES))
from DOCUMENT
where DOC_ID = '12345'
Can someone please help in simplifying it and reading both the values.
Thanks in advance.
First, your query looks fine for email but it has incorrect syntax, second just use left() with charindex() to get the first part :
select left(notes, charindex(';', notes)-1),
substring(notes, charindex(';', notes)+1, len(notes))
from document
where doc_id = 12345;

string between two special characters impala sql

Hi all I am trying to write sql for selecting string between two special characters.
example: in the table, field value like 7185878969-129981041-000000 . how can I select only middle portion 129981041 without hard coding. What will be the best way to go about this?.Please provide sample code. Thanks
Impala has split_part():
select split_part(col, '-', 2)
Try this for MySQL:
SELECT REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(Column,'-',2)),'-',1))
FROM table_name;
Result:
129981041

Oracle db query - data format issue

I'm wondering if there is a way to query the oracle db against formatted field value.
Example:
I have a table of postcodes stored in the format of "part1 part2". I want to be able to find a postcode either by searching it using the above format or "part1part2" format.
What I was thinking is to format the entered postcode by removing the spaces and then query the database like:
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode.**Format(remove spaces)** = 'part1part2'
The Format(remove spaces would convert the postcode from "part1 part2" to "part1part".
My question is, is it possible?
You can use regexp_replace
SELECT *
FROM POSTCODES_TBL t
WHERE regexp_replace(t.postcode,'\s', '') = 'part1part2'
This will remove any whitespace (space, tab, newlines etc)
or if you only want to get rid of spaces, replace will work just as well:
SELECT *
FROM POSTCODES_TBL t
WHERE replace(t.postcode,' ', '') = 'part1part2'
More details in the manual:
replace
regexp_replace
You could use like
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode like 'part1%part2'

SQL Query: data with comma

Im using sql server 2005. i want data with comma. for example ['5000'],['5001'],..
but the last record should not include comma. Pls help me.
Query:
select '['''+convert(varchar,parcelid)+'''],' from sampletable
Try the COALESCE function
SELECT #groupedText = COALESCE(#groupedText, '') + [Text] + ','
FROM Requirement
WHERE CampaignId = #campaignId
ORDER BY [Text]
Then you could try one of the string functions to kill the end comma
T-SQL string functions
You can use regular expressions to remove the last comma or do it using your programming language (ASP etc. like a chop function or something).
http://weblogs.sqlteam.com/jeffs/archive/2007/04/27/SQL-2005-Regular-Expression-Replace.aspx
Consider using XML for this purpose. The "aggregate concatenation" solution may not be reliable, because it is not clearly documented and supported. You can get rid of the final comma with SUBSTRING, as boon suggested.
See this thread.