How to create a Pipe delimited text file in stored procedure - sql-server

I need to create a stored procedure that would create a pipe delimited text file based on user requirements.
The table that I will use has only 6 columns with names different from user required fields.
Also, the number of columns that user wants is 23. Some of them we do not have data for. I just need to display them in the text file.
I'm not sure how to display the data lined up under appropriate column while skipping other optional columns.
I think I would need something like this:
OptionalColumn 1|DataColumn 1|OptionalColumn 2|DataColumn 2
12/12/2015 Name 1
12/12/2015 Name 2
Or some other formatting for pipe delimited file.
How would I approach this?
Never done something like this.

You could probably concatenate your fields with a simple select like
SELECT '|' + YOUR_COLUMN_NAME
If you need more elaborate selections then perhaps this Stack Overflow approach may give you ideas Comma Separated results in SQL
The example creates a comma separated list, but the principle is the same. It allows for concatenation of data from multiple rows for a same id.

Seems like you might be able to just use concatenation if your output is always consistent....
Select 'col1|col2|col3|coln....'
union
Select '' as extracol + '|' + realcol + '|' + '' as extracol2 + '|' + realcol2 +'...';
..which will produce a String with headers
if you need to discover the structure dynamically that's a different story, you'll have to use the system tables, but this might be a simple solution. Also if you have a ton of rows, this is not a great approach. If your app is going to stream entries to a file, it may be best to have your app create the file format, not the database stored proc

Related

Customize Normalization in SQL Server Full Text Search by replacing characters

I want to customize SQL Server FTS to handle language specific features better.
In many language like Persian and Arabic there are similar characters that in a proper search behavior they should consider as identical char like these groups:
['آ' , 'ا' , 'ء' , 'ا']
['ي' , 'ی' , 'ئ']
Currently my best solution is to store duplicate data in new column and replace these characters with a representative member and also normalize search term and perform search in the duplicated column.
Is there any way to tell SQL Server to treat any members of these groups as an identical character?
as far as i understand ,this would be used for suggestioning purposes so the being so accurate is not important. so
in farsi actually none of the character in list above doesn't share same meaning but we can say they do have a shared short form in some writing cases ('آ' != 'اِ' but they both can write as 'ا' )
SCENARIO 1 : THE INPUT TEXT IS IN COMPLETE FORM
imagine "محمّد" is a record in a table formatted (id int,text nvarchar(12))named as 'table'.
after removing special character we can use following command :
select * from [db].[dbo].[table] where text REPLACE(text,' ّ ','') = REPLACE(N'محمد',' ّ ','');
the result would be
SCENARIO 2: THE INPUT IS IN SHORT FORMAT
imagine "محمد" is a record in a table formatted (id int,text nvarchar(12))named as 'table'.
in this scenario we need to do some logical operation on text before we query in data base
for e.g. if "محمد" is input as we know and have a list of this special character ,it should be easily searched in query as :
select * from [db].[dbo].[table] where REPLACE(text,' ّ ','') = 'محمد';
note:
this solution is not exactly a best one because the input should not be affected in client side it, would be better if the sql server configure to handle this.
for people who doesn't understand farsi simply he wanna tell sql that َA =["B","C"] and a have same value these character in the list so :
when a "dad" word searched, if any word "dbd" or "dcd" exist return them too.
add:
some set of characters can have same meaning some of some times not ( ['ي','أ'] are same but ['آ','اِ'] not) so in we got first scenario :
select * from [db].[dbo].[table] where text like N'%هی[أي]ت' and text like N'هی[أي]ت%';

Snowflake:Export data in multiple delimiter format

Requirement:
Need the file to be exported as below format, where gender, age, and interest are columns and value after : is data for that column. Can this be achieved while using Snowflake, if not is it possible to export data using Python
User1234^gender:male;age:18-24;interest:fishing
User2345^gender:female
User3456^age:35-44
User4567^gender:male;interest:fishing,boating
EDIT 1: Solution as given by #demircioglu
It displays as NULL values instead of other column values
Below the EMPLOYEES table data
When I ran below query
SELECT 'EMP_ID'||EMP_ID||'^'||'FIRST_NAME'||':'||FIRST_NAME||';'||'LAST_NAME'||':'||LAST_NAME FROM tempdw.EMPLOYEES ;
Create your SQL with the desired format and write it to a file
COPY INTO #~/stage_data
FROM
(
SELECT 'User'||User||'^'||'gender'||':'||gender||';'||'age'||':'||age||';'||'interest'||':'||interest FROM table
)
file_format = (TYPE=CSV compression='gzip')
File format here is not important because each line will be treated as a field because of your delimiter requirements
Edit:
CONCAT function (aliased with ||) returns NULL if you have a NULL value.
In order to eliminate NULLs you can use NVL2 function
So your SQL will have series of NVL2s
NVL2 checks the first parameter and if it's not NULL returns first expression, if it's NULL returns second expression
So for User column
'User'||User||'^' will turn into
NVL2(User,'User','')||NVL2(User,User,'')||NVL2(User,'^','')
P.S. I am leaving up to you to create the rest of the SQL, because Stackoverflow's function is to help find the solution, not spoon feed the solution.
No, I do not believe multiple delimiters like this are supported in Snowflake at this time. Multiple byte and multiple character delimiters are supported, but they will need to be specified as the same delimiter repeated for either record or line.
Yes, it may be possible to do some post-processing or use Python scripts to achieve this. Or even SQL transformative statements. This is not really my area of expertise so if someone has an example for you, I'll let them add to the discussion.

"EmptyHeader" in CSV Export Options?

I have a CSV file I am attempting to create, and the recipient requires a header row. In this header row (and in the data) there is a field that used to be present that was removed. However, they did not remove the column that that held that data, so now, there is an empty column name surrounded by delimiters ("|"). How can I recreate this?
The expected results for the following columns should be:
RxType1|RxType2|RxType3|RxType4|RxType5||DelivID
(There is an empty column between RxType5 and DelivID) and the results would be:
|Rx|OTC|Legend|Generic|Other||Express
I am using SSRS, and have attempted adding an extra pipe the the column header for RxType5 with an empty column behind it, but the CSV seems to generate a header row based on the column names from the stored procedure and not from the RDL data. I have also attempted in the Stored Proc to create the column by using:
Select
'' AS ""
OR
'' AS "|"
but when I refresh the fields in SSRS, it puts that the column is called "ID_" (because a space, no character, or pipe is non-CLS compliant.
Any suggestions on how I can achieve this? Thanks so much :)
Try creating the column with a known name, like SELECT '' AS [RemoveMe], and then just remove that name from the row header text box.

SQL - Including whitespace in LIKE query for filtering content include swear words

I have a table of swear words in SQL Server and I use LIKE query to search texts for words in the table. I need a way to include whitespaces around the swear word in LIKE query, like this:
... LIKE '%{whitespace}SWEAR-WORD{whitespace}%';
Putting space around the swear word is not enough, because it can be a part of another normal word in my language (like 'inter' that is part of 'international' or 'pointer').
Another solution I've tried was using this:
... LIKE '%[^a-zA-Z]SWEAR-WORD[^a-zA-Z]%';
But that did not work for me.
Is there any way to do this? Or alternatively is there any solution other than LIKE query?
Edit: For better understanding, it's our current way to find swear-words:
We have a table named Reviles which has 2 columns (Id and Text) and contains restricted words and phrases. We use this query to find out whether a content has any of those restricted words and phrases:
IF EXISTS (SELECT * dbo.Reviles WHERE #Text LIKE '%' + dbo.Reviles.Text + '%')
#IsHidden = 0
Note that this check is done before the content being inserted into its table. The code above is part of a stored procedure which gets information of a post and checks various things including swear words before inserting it.
Before we've stored restricted words like ' swear-word ' in the table, however this way we could not find and hide contents with swear words at the beginning or at the end of the line or contents which consists of only a swear word. For example:
This is my content with a swear-word
or
Swear-word in my content
or
Swear-word
So we decided to remove those spaces and store restricted words like 'swear-word'. But this causes some normal content to hide because some swear words can be part of another word which is normal (If we assume inter is a bad word, then pointer and international, etc. will be restricted).
Sorry for my bad English, I hope with this description, I've made it clear.
try to close your check statement in some chars and then compare:
some data:
declare #T table(stmt nvarchar(20))
insert into #T values ('inter'),('Inter.'),('My inter'),
('intermediate!'),('pointer '),('Good inter'),('inter inter inter')
try this:
select
stmt as stmt,
case
when '.'+stmt+'.' like '%[^a-z]inter[^a-Z]%' then 1 else 0 end as [has inter]
from
#T
results:
stmt has inter
-------------------- -----------
inter 1
Inter. 1
My inter 1
intermediate! 0
pointer 0
Good inter 1
inter inter inter 1
I'm a bit confuse what are u want to do, if u want to do like '{whitespace}swearword{whitespace}', then use like '% inter %' already work
but if u really have special requirement about filter, another way is enable SQL CLR, and Create Sql function from visualStudio and deploy to SQL Server. inside SQL function u can use Regular expression to return match or not.
Create SQL Databaase Project
Add SQL CLR(I use C#)
Add Code
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlBoolean RegularMatch(string str, string pattern)
{
var regex = new Regex(pattern);
return new SqlBoolean (regex.IsMatch(str));
}
}
Public to SQL Server
Sorry I'm not good at format this.

Word wrap issues with SSIS Flat file destination

Background: I need to generate a text file with 5 records each of 1565 character length. This text file is further used to feed the data to a software.
Hence, they are some required fields and optional fields. I created a query with all the fields added together to get one single field. I populated optional fields with a blank.
For example:
Here is the sample input layout for each fields
Field CharLength Required
ID 7 Yes
Name 15 Yes
Address 15 No
DOB 10 Yes
Age 1 No
Information 200 No
IDNumber 13 Yes
and then i generated a query for each unique ID with the above fields into a single row which looks like following:
> SELECT Cast(1 AS CHAR(7))+CAST('XYZ' AS CHAR(15))+CAST('' AS CHAR(15))+CAST('22/12/2014' AS
CHAR(10))+CAST('' AS CHAR(1))+CAST(' AS CHAR(200))+CAST('123456' AS CHAR(13))
UNION
SELECT Cast(2 AS CHAR(7))+CAST('XYZ' AS CHAR(15))+CAST('' AS CHAR(15))+CAST('22/12/2014' AS
CHAR(10))+CAST('' AS CHAR(1))+CAST(''AS CHAR(200))+CAST('123456' AS CHAR(13))
Then, I created an SSIS package to produce the output text file through Flat file destination delimited.
Problem:
Even though the flat file is generated as per the desired length(1565). The text file looks differently when the word wrap is ON or OFF. When Word wrap is off , i get the record in single line. If the Word wrap is on, the line is broken into multiple. the length of the record in either case is same.
Even i tried to use VARCHAR + Space in the query instead of CHAR for each field, but there is no success. Its breaking the line for blank fields.
For example: Cast('' as varchar(1)) + Space(200-len(Cast('' as varchar(1)))) for Information field
Question: How do make it into a single line even though the word wrap is ON.
Since its my first post, please excuse me for format of the question
The purpose of word wrap is to put characters on the next line in instances of overflow rather than creating an extremely horizontal scrolling document.
Word wrap is the additional feature of most text editors, word processors, and web browsers, of breaking lines between words rather than within words, when possible.
Because this is what word wrap is there's nothing you can do to change its behavior. What does it matter anyway? The document should still be parsed as you would expect. Just don't turn word wrap on.
As far as I'm aware, having word wrap on or off has no impact on the document itself, it's simply a presentation option.
Applications parsing a document parse it as if word wrap were off. Something that could throw off parsing is breaks for a new line, but that is a completely different thing from word wrap.

Resources