How convert string "20080926112720" to datetime in SSIS? - sql-server

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 )

Related

Snowflake - Setting Date Time format in result of Query

I'm running a query in snowflake to then export. I need to set/convert a date value to the following format 'yyyy-MM-ddThh:mm:ss' from 2022-02-23 16:23:58.805
I'm not sure what is the best way to convert the date format. I've tried using TO_TIMESTAMP, but keep getting the following error '1 too many arguments for function [TO_TIMESTAMP(FSA.LAST_UPDATED, 'yyyy-MM-ddThh:mm:ss')] expected 1, got 2'
This looks like a conversion issue. Please check datatype for your column last_updated. Also seems there is some typo in your question - for the time portion in format, use mi (hh:mi:ss).
Refer below -
select to_timestamp('2022-02-23 16:23:58.805'::TIMESTAMP,'yyyy-mm-dd hh:mi:ss.ff')
;
000939 (22023): SQL compilation error: error line 1 at position 7
**too many arguments for function
[TO_TIMESTAMP(TO_TIMESTAMP_NTZ('2022-02-23 16:23:58.805'), 'yyyy-mm-dd hh:mi:ss.ff')] expected 1, got 2**
select to_timestamp('2022-02-23 16:23:58.805'::string,'yyyy-mm-dd hh:mi:ss.ff');
TO_TIMESTAMP('2022-02-23 16:23:58.805'::STRING,'YYYY-MM-DD HH:MI:SS.FF')
2022-02-23 16:23:58.805
TO_TIMESTAMP is for string -> timestamp, TO_CHAR is for timestamp -> string of which the TO_CHAR( <date_or_time_expr> [, '<format>' ] ) form is the one you seem to be wanting.
this SQL show string -> timestamp -> formatted string
SELECT
'2022-02-23 16:23:58.805' as time_string,
to_timestamp(time_string) as a_timestamp,
to_char(a_timestamp, 'yyyy-MM-ddThh:mm:ss') as formating_string;
TIME_STRING
A_TIMESTAMP
FORMATING_STRING
2022-02-23 16:23:58.805
2022-02-23 16:23:58.805
2022-02-23T16:02:58

Use result of sql query to replace text on multiple xml files

i have a table on sql like this:
CD_MATERIAL | CD_IDENTIFICACAO
1 | 002323
2 | 00322234
... | ...
AND SO ON (5000+ lines)
I need to use that info to search and replace multiple external xml files on a folder (all the tags on those XML had numbers like the CD_IDENTIFICACAO from sql query, i need to replace with corresponding cd_material from sql query "ex.: 002323 becomes 1)
I used this query to extract all the cd_identificacao to use on Notepad++:
declare #result varchar(max)
select #result = COALESCE(#result + '', '') + CONCAT('(',CD_IDENTIFICACAO,')|') from TBL_MATERIAIS WHERE CD_IDENTIFICACAO <> '' ORDER BY CD_MATERIAL
select #result
That would bring me ex.:
(1TEC45D025)|(1TEC800039)|(999999999)|(542251)|(2TEC58426)|(234852)
and changed the parameters to get the replace ex.:
(? 2000)|(? 2001)|(? 2002)|(? 2003)|(? 2004)|(? 2005)
but i don't know how to add a number (increment) on front of "?" so notepad++ would understand it (search and replace would have 5000+ results, so it's not pratical to manually add the increment).
I was able to get a workaround for this. I've used this query to get all the the terms for find and replace i needed (1 per line)
select concat('<cProd>',cd_identificacao,'</cProd>'), concat('<cProd>',cd_material,'</cProd>') from tbl_materiais where cd_identificacao <> '' order by cd_material
That would result in:
<cProd>1TEC460054</cProd> <cProd>1</cProd>
<cProd>1TEC240035</cProd> <cProd>2</cProd>
(i added the tag too to make sure no other information could be replaced as there were many number combinations that could lead to incorrect replacement)
then pasted it on a txt and i used the notepad++ to replace the space between column 1 and 2 for /r/n wich would result in:
<cProd>1TEC460054</cProd>
<cProd>1</cProd>
<cProd>1TEC240035</cProd>
<cProd>2</cProd>
then i used "Ecobyte Replace Text" Tool, pasted my result file as new selection in bottom frame, loaded all my files on a new replace group on top frame (on properties of the group, u can change directory and options), then executed the replacement, it worked perfectly.
Thx.

Splitting contents of one sql column into 3 columns based on certain characters that always happen in the value

I'm trying to form a SQL query, using SQL Server 2014 without creating a function. I do not have permissions on the database to create functions so I have to do it with a query only.
I have a column named Test with the example value of:
Accounting -> Add Missing functionality in Payable -> Saving a blank Missing row
I want my query to return the information (of varying length) between the two arrows (->). I have tried the right, left, substring, charindex and patindex functions and various combinations of each.
Basically the query needs to be SUBSTRING(Test, CHARINDEX(' -> ', TEST) +3, <some length here>)
The length is the part I'm having a hard time figuring out. I need the full length minus the first part before and including the first -> which evaluates to:
Add Missing functionality in Payable -> Saving a blank Missing row
From that result, I need to remove everything after and including the ->, which would then leave me with:
Add Missing functionality in Payable
At the end of the day, I want to split this one column up into 3 like so:
Domain | Feature | Test
------------------------------------------------------------------------------
Accounting | Add Missing functionality in Payable | Saving a blank Missing row
Can anyone show me how to do this query, without having to write a function? Any suggestions would be greatly appreciated as I have been working on this one portion of the query for the better part of 4 hours now. Thank you in advance for your help. Have a great day!!
I tried the following query and it is woking fine for me:
DECLARE #X as varchar(1000)
SET #X = 'Accounting -> Add Missing functionality in Payable -> Saving a blank Missing row'
SELECT SUBSTRING(#X,1,CHARINDEX('->',#X) - 1) AS Domain,
SUBSTRING(#X,CHARINDEX('->',#X) + 2,LEN(SUBSTRING(#X,CHARINDEX('->',#X) + 2,LEN(#X))) - LEN(SUBSTRING(#X,LEN(#X) - CHARINDEX('>-',REVERSE(#X)) ,LEN(#X)))) AS Feature,
SUBSTRING(#X,LEN(#X) - CHARINDEX('>-',REVERSE(#X)) + 2 ,LEN(#X)) AS Test
You have to use this query:
SELECT SUBSTRING([Test],1,CHARINDEX('->',[Test]) - 1) AS Domain,
SUBSTRING([Test],CHARINDEX('->',[Test]) + 2,LEN(SUBSTRING([Test],CHARINDEX('->',[Test]) + 2,LEN([Test]))) - LEN(SUBSTRING([Test],LEN([Test]) - CHARINDEX('>-',REVERSE([Test])) ,LEN([Test])))) AS Feature,
SUBSTRING([Test],LEN([Test]) - CHARINDEX('>-',REVERSE([Test])) + 2 ,LEN([Test])) AS Test
FROM MyTable --Replace MyTable with your table name

Query for pattern separated by new lines

I have a table (defect ) where a column stores a text. Each line in this text represents a version. (this is clearquest database running microsoft SQL, accessed via JDBC)
For example, following data represents three versions a fix is made.
defect version_fixed
1 2015.1.1
2 2015.1.1\n2015.1.13
3 2015.1.12\n2015.1.1
4 2015.1.12\n2015.1.1\n2015.1.13
5 2015.1.13\n2015.1.10
5 2015.1.100
As you see the version is not stored in an order. It can appear anywhere.
I am interested in all rows with fix version fixed containing "2015.1.1". But my query either gets more rows or skips some
version_fixed like '%2016.1.1%' (gets row 5 as it matches the pattern)
version_fixed like '%2016.1.1\n'(does not get any thing.)
I am looking for query to get exact list for 2015.1.1
defect version_fixed
1 2015.1.1
2 2015.1.1\n2015.1.13
3 2015.1.12\n2015.1.1
4 2015.1.12\n2015.1.1\n2015.1.13
How can I query where text matches with "exact string, delimited by new line or end of text". What is the correct way to escape new line?
Side note: Current solution is to get all records(including unwanted one and then filter out incorrect results)
You could try this. It relies on Sql Server adding the newline to the string when you break the line.
create table defect( version_fixed varchar(max) )
insert into defect( version_fixed )
values ( '2015.1.1' )
, ( '2015.1.1
2015.1.13' )
, ( '2015.1.12
2015.1.1' )
, ( '2015.1.12
2015.1.1
2015.1.13')
, ( '2015.1.13
2015.1.10' )
, ( '2015.1.100' )
-- break to a new line and Sql Server will include the newline character in the string
select * from defect where version_fixed like '%2015.1.1
%' or version_fixed like '%2015.1.1'
You can as the below:
WHERE '\' + version_fixed + '\' LIKE '%2015.1.1\%'
This solution depands on your sample data.

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