SSIS Find & Reverse Function - sql-server

I am trying to get the date from the below filename using ssis expression.i get this file name from the variable
I have tried using findstring and reverse function but it still throwing cannot parse.
Any recommendtions pls
Variable
#[User::CurrentFile] : Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx
Need to get 20170327 and convert to 2017-03-27
Thanks in advance

You can use LEFT and RIGHT and SUBSTRING to achieve this, use the following expression to get the expected output:
SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),1,4) + "-" + SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),5,2) + "-" + SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),7,2)
If #[User::CurrentFile] = 'Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx' you will get 2017-03-27

Try something like below, may be in a exec sql task or script and assign it to a variable for using further.
select Cast(CONVERT(date,
Reverse(Substring(Reverse('Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx'),
CHARINDEX('.',Reverse('Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx'),1)+1,
8))) as varchar(10))

Related

Custom ms sql query

Is it posible that an ms sql query to return only a portion of what is stored in a field?
For example, I got this data stored in the field:
row NGV1="" NGV10="*" NGV6=" " NGV5=" " NGV4=" " NGV3=" " NGV2=" " _tipprodus="NGV" lotuloptim="20" /
I only need to display the value that is between the quotation marks from this part lotuloptim="20". The result should be 20.
Thank you in advance.
Best regards
Yes, you can use SUBSTRING, but using substring you need to know the start index
SELECT SUBSTRING(column_name,start,length) AS some_name FROM table_name;
and then you can use CHARINDEX to find the index for example to the lotuloptim word
DECLARE #document varchar(64);
SELECT #document = 'Reflectors are vital safety' +
' components of your bicycle.';
SELECT CHARINDEX('bicycle', #document);
GO

How to replace escape character in Netezza column

I am trying to replace escape character in Netezza column, but it is not properly replacing.
Please help me some one on this.
select replace('replaces\tring','\','\\\\');
I need output as replaces\\\\tring. Below is the error message i am getting...
ERROR [42S02] ERROR: Function 'REPLACE(UNKNOWN, UNKNOWN, UNKNOWN)'
does not exist Unable to identify a function that satisfies the given
argument types You may need to add explicit typecasts
Thanks in advance.
This is because REPLACE function needs to be installed (which is not by default). There is another function which is called TRANSLATE which can be used in a limited way instead of REPLACE but unfortunately won't fit in your situation.
You can use the below query instead:
SELECT SUBSTRING(x, 1, INSTR(x, '\') - 1) || '\\\\' || SUBSTRING(x, INSTR(x, '\') + LENGTH('\')) FROM
(SELECT 'replaces\tring' AS x) t
\ passed to INSTR and LENGTH is the string to be replaced. Note that they occur in three positions.
\\\\ in the middle is the replacement string.
replaces\tring is the string to search in.
Check the below example for replace love with like in I love Netezza:
SELECT SUBSTRING(x, 1, INSTR(x, 'love') - 1) || 'like' || SUBSTRING(x, INSTR(x, 'love') + LENGTH('love')) FROM
(SELECT 'I love Netezza' AS x) t
That particular function is part of the "SQL Extensions toolkit" and on our system it is placed in the ADMIN schema of the SQLEXT database. All users have been granted execute access to that schema. Furthermore the database.schema have been placed in the path (the DBA's did it globally, but you can issue a "set PATH=..." in your session if need be)
our path is:
select current_path;
CURRENT_PATH
---------------------------------------------------------------------------------------
SQLEXT.ADMIN,INZA.INZA,NZA.INZA,NZM.INZA,NZMSG.INZA,NZR.INZA,NZRC.INZA,SYNCHDB.ADMIN
and as you can see the SQLEXT is at the beginning...

Why does my SQL query fail when I use quotes?

Hi I want to write sql query in MVC4? I will explain my issue clearly
Sql Query
Select * from View_VisitorsForm where VisitingDate>='' and VisistingDate<=''
I tried to write this query in controller but i am getting error. i donno the perfect syntax to use in controller. My error which is mentioned in the below image.
Please any one tell me the correct solution for my problem.
Advance Thanks.
I think what you are looking for is the following:
SqlCommand cmd = new SqlCommand("Select * from view_VisitorsForm where VisitingDate between " + fromdt + " and " + todt + "",con);

How convert string "20080926112720" to datetime in SSIS?

i am getting the value from xml 20080926172720. I have loaded using ssis as a string column in sql server.but ineed to store 09/26/2008 27:17:20 like this as a datetime in sql server
convert(datetime,'20080926112720',101)
I need a output like this
09/26/2008 11:27:20
I'm using SQL Server 2005
It's not the cleanest way but SQL is having trouble parsing out the HH:mm:ss without the colon separator. This way will get you the formatting you want. I strongly suggest finding a way to make your input to the convert function formatted as 'CCYYMMDD HH:mm:ss'
DECLARE #MyDate char(15)
SET #MyDate = '20080926132720'
SELECT SUBSTRING(#Mydate,5,2)+'/'+SUBSTRING(#MyDate,7,2)+'/'+SUBSTRING(#MyDate,1,4)+SPACE(1)+SUBSTRING(#MyDate,9,2)+':'+SUBSTRING(#MyDate,11,2)+':'+SUBSTRING(#MyDate,13,2)
I can't believe I put the amount of time into this that I did but you need to create a new variable and evaluate the variable as an expression to give it the proper formatting. Right now SSIS has no clue how to parse the blurb of text you are giving it. In the expression builder you should build something like this.
SUBSTRING( "20080926112720",1, 4 )+"-"+SUBSTRING( "20080926112720",5, 2 )+"-"+SUBSTRING( "20080926112720",7, 2 )+ " "+SUBSTRING( "20080926112720",9, 2 )+ ":"+SUBSTRING( "20080926112720",11, 2 )+":" +SUBSTRING( "20080926112720",13, 2 )
This will evaluate to this value 2008-09-26 11:27:20 then you can set that variable as Datetime.
You will of course need to modify this as
SUBSTRING( ##YourDateVariable,1, 4 )+"-"+SUBSTRING( ##YourDateVariable,5, 2 )+"-"+SUBSTRING( ##YourDateVariable,7, 2 )+ " "+SUBSTRING( ##YourDateVariable,9, 2 )+ ":"+SUBSTRING( ##YourDateVariable,11, 2 )+":" +SUBSTRING( ##YourDateVariable,13, 2 )

Can't Convert unicode Data into XML column in sql server 2008

I already have a table with 3 column (Id bigint, Title nvarchar(450), Description nvarchar(MAX)) in sql 2008
I decide convert Title and Description column into one XML column. but when trying to update get many error like "illegal name character" or "illegal qualified name character" and etc.
to solve this problem i just create windows application with subsonic 2.1 with below code
MyTable tbl = new MyTable(1111);
tbl.myXMLcol = "<Person><Title><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Title) + " ]]></Title><Description><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Description) + " ]]></Description></Person>";
tbl.Save();
then try to add "<?xml version=\"1.0\" encoding=\"utf-16\"?>" into first above string that get error "unable to switch the encoding".
Note: Also i using this method to remove illegal character, but not solve my problem
Note2: I trying to update Japanese record that get this error, but for English work properly.
Could you please help me.
Thanks.
I think you should be using UTF-8 encoding.
You can find out more about the encoding here:
http://www.ibm.com/developerworks/xml/library/x-utf8/
Also, you will find some more information here:
http://msdn.microsoft.com/en-us/library/ms131375.aspx
Why not do all of this directly in SQL Server ?? Could you try this:
UPDATE YourTable
SET XmlCol = CAST(N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>' AS XML)
WHERE ID = 12345
Does that work? If not, then maybe this one here?
UPDATE YourTable
SET XmlCol = CONVERT(XML, N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>', 2)
WHERE ID = 12345
The "2" for style will strip out things SQL Server doesn't like about XML - things like DTD and so forth. Maybe that'll help in your case, too?
I've had various troubles with SQL Server's XML support when importing XML from outside. Usually, one way or another, it ends up working, though :-)
Marc
After many research, I found article about my problem.
Please see Tarun Ghosh post

Resources