I am modifying a system that was already made, to insert and show columns is being done with a tNGs library, I don't really understand how it works.
The problem is that there is a line of code where a date column is added and inserted
ins_Tis_ServiciosPrev.addColumn "fecha", "DATE_TYPE", "POST", "fecha", ""
when inserting this, the error is for the date, example:
I set the date to
12/12/2021
and when inserting it becomes like this:
2021/12/2012
If to the code to add the column I leave it like this:
ins_Tis_ServiciosPrev.addColumn "fecha", "STRING_TYPE", "POST", "fecha", ""
So the date becomes:
2021/12/12
But I want:
12/12/2021
Any thoughts on this?
This is my complete code:
<%
' Make an insert transaction instance
Dim ins_Tis_ServiciosPrev: Set ins_Tis_ServiciosPrev = new tNG_multipleInsert
ins_Tis_ServiciosPrev.init MM_oConn_STRING
tNGs.addTransaction ins_sola
' Register triggers
ins_Tis_ServiciosPrev.registerTrigger Array("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1")
ins_Tis_ServiciosPrev.registerTrigger Array("BEFORE", "Trigger_Default_FormValidation", 10, formValidation)
ins_Tis_ServiciosPrev.registerTrigger Array("END", "Trigger_Default_Redirect", 99, "cerrar.asp")
' Add columns
ins_Tis_ServiciosPrev.setTable "sola"
ins_Tis_ServiciosPrev.addColumn "otrocontexto", "STRING_TYPE", "POST", "otrocontexto", ""
ins_Tis_ServiciosPrev.addColumn "fecha", "STRING_TYPE", "POST", "fecha", ""
ins_Tis_ServiciosPrev.addColumn "hora", "STRING_TYPE", "POST", "hora", ""
ins_Tis_ServiciosPrev.setPrimaryKey "id", "NUMERIC_TYPE", "", ""
%>
But I want: 12/12/2021
Any thoughts on this?
You're not working directly with ADO, so we have no idea what
ins_Tis_ServiciosPrev.addColumn "fecha", "DATE_TYPE", "POST", "fecha", ""
does or why it's not working for you.
But sending the date as a string can be made to work.
When sending a date as a string to SQL Server use the format YYYYMMDD, which will be converted correctly in every culture.
If that doesn't work, you may have to find the source code for tNG_multipleInsert and post that.
Related
I am now working on loading some data from Snowflake using the REST API called SQL API. The issue is that Snowflake uses some weird format for fields with DATE type when creating the response JSON.
I have this example field metadata:
{
"name" : "...",
"database" : "...",
"schema" : "...",
"table" : "...",
"type" : "date",
"scale" : null,
"precision" : null,
"length" : null,
"collation" : null,
"nullable" : true,
"byteLength" : null
}
And in the resultset its value is "9245". Using the query in the browser I see that the actual value is 1995-04-25.
What magic function decodes this integer back to a date?
Based on documentation Getting the Data From the Results
DATE
Integer value (in a string) of the number of days since the epoch (e.g. 18262).
Related: Why is 1/1/1970 the “epoch time”?
Check:
SELECT DATEADD(day, 9245, '1970-01-01'::DATE)
--1995-04-25
SELECT '1970-01-01'::DATE + INTERVAL '9245 DAYS';
-- 1995-04-25
db<>fiddle demo
Redesign of webpage problem - I have a field in my SQL database called Approved. This field was either true for approved, or false for not approved. Now, the user would like to track denied. So, it's approved - true, not approved - false, or not decided -null.
I found out that SQL database bit field can also be null. So, it can keep the values true, false, or null. In SQL, I tested it, and the field can be null.
My problem - on my webpage, I'm added a record to this table (not even mentioning that field) and it automatically enters that field as it as false. I have tried to add null to that field, but it still goes into sql as false. Is there a way to do this with a bit field or do I have to create a new field (please say there's a way to do this)? Here's my code for trying to add the field as null:
Using connection As New SqlConnection("Data Source=WillSQL\ict2;Initial Catalog=TimeSQL_testing;Integrated Security=SSPI ")
commandText = "Insert into tblWorkHours(Employee, EmployeeName, DateRequested, WorkCode, BeginDateOff, EndDateOff, AllDay_YesNo, approved, sys_usr, stampaddtime) values (#Employee,#EmployeeName,#DateRequested,#WorkCode,#BeginDateOff,#EndDateOff,#AllDay_YesNo, #approved, #sys_user, #stampaddtime)"
Dim cmd As New SqlCommand(commandText, connection)
connection.Open()
cmd.Parameters.AddWithValue("#Employee", stempid)
cmd.Parameters.AddWithValue("#EmployeeName", stempname)
cmd.Parameters.AddWithValue("#DateRequested", stdtreq)
cmd.Parameters.AddWithValue("#WorkCode", streason)
cmd.Parameters.AddWithValue("#BeginDateOff", stdtbeg)
cmd.Parameters.AddWithValue("#EndDateOff", stdtend)
cmd.Parameters.AddWithValue("#AllDay_YesNo", stallday)
cmd.Parameters.AddWithValue("#approved", vbNull)
cmd.Parameters.AddWithValue("#sys_user", vname)
cmd.Parameters.AddWithValue("#stampaddtime", dt)
cmd.ExecuteNonQuery()
connection.Close()
End Using
Thanks!
To set a database value to NULL from .net, you need to use dbnull.value:
So change this:
cmd.Parameters.AddWithValue("#approved", vbNull)
To this:
cmd.Parameters.AddWithValue("#approved", DbNull.Value)
I am currently running a job to transfer data from one table to another via a query. But I can't seem to find a way convert a column into a nested field containing the column as a child field. For example, I have a column customer_id: 3 and I would like to convert it to {"customer": {"id":3}}. Below is a snippet of my job data.
query='select * FROM ['+ BQ_DATASET_ID+'.'+Table_name+'] WHERE user="'+user+'"'
job_data={"projectId": PROJECT_ID,
'jobReference': {
'projectId': PROJECT_ID,
'job_id': str(uuid.uuid4())
},
'configuration': {
'query': {
'query': query,
'priority': 'INTERACTIVE',
'allowLargeResults': True,
"destinationTable":{
"projectId": PROJECT_ID,
"datasetId": user,
"tableId": destinationTable,
},
"writeDisposition": "WRITE_APPEND"
},
}
}
Unfortunately, if the "customer" RECORD does not exist in the input schema, it is not currently possible to generate that nested RECORD field with child fields through a query. We have features in the works that will allow schema manipulation like this via SQL, but I don't think it's possible to do accomplish this today.
I think your best option today would be an export, transformation to desired format, and re-import of the data to the desired destination table.
a simple solution is to run
select customer_id as customer.id ....
I have a task to remove a JSON property saved in a SQL Server database table's column. Here is the structure of my table
OrderId Name JSON
In the JSON column I have this JSON data:
{
"Property1" :"",
"Property2" :"",
"metadata-department": "A",
"metadata-group": "B"
}
I have over 500 records that have this json value.
Can I update all the records to remove metadata-department?
This question is old but I thought I'd post another solution for those who may end up here via search engine.
In SQL Server 2016 or SQL Azure, the following works:
DECLARE #info NVARCHAR(100) = '{"name":"John","skills":["C#","SQL"]}'
PRINT JSON_MODIFY(#info, '$.name', NULL)
-- Result: {"skills":["C#","SQL"]}
Source: https://msdn.microsoft.com/en-us/library/dn921892.aspx
In my SQL Server environment, I have installed CLR RegEx Replace functions and using these, I've achieved what you wanted:
DECLARE #Text NVARCHAR(MAX) = '{
"Property1" :"",
"Property2" :"",
"metadata-department": "A",
"metadata-group": "B"
}';
PRINT dbo.RegExReplace(#Text, '"metadata-department": "[A-Za-z0-z]",\r\n\s+', '');
Result:
{
"Property1" :"",
"Property2" :"",
"metadata-group": "B"
}
CLR Source: https://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/
in MYSQL it's JSON_REMOVE()
UPDATE orders SET JSON=JSON_REMOVE(JSON, $."metadata-department")
https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-remove
First off I would like to say that I think that my question may be time consuming for people to solve it in a complete sense so I understand that it is totally possible that the complete answer is asking for just way too much, so anything to help me better understand, like: reading material, examples, links, and/or advice would be great and I do very much appreciate any and every comment I receive, good or bad it just makes me and this place alot better, finally, I would like to thank you all so much for everything that you do here, this is truly a place that was build by smart people, and people that care.
MY QUESTION
(using Classic ASP and SQL Server)
I know that it is possible to read a remote XML file and then insert it into a SQL Server database table. I found one example that does that it uses Classic ASP and MS Access but that can be changed to SQL Server with minimal coding the URL is: http://forums.aspfree.com/code-bank-54/classic-asp-import-remote-xml-file-into-database-152966.html
But the problem is that I cannot get it to work on my remote XML file, I tried to edit the classic asp code (found in the link above) for days and days and I just cannot get it to work the way I would like.
So I want to start from the beginning,
The XML file in question is located on: http://www.iftach.org/taxmatrix/charts/2Q2012.xml
I seen a couple of examples on how you can export the entire xml file into the SQL Server database (like do a BULK insert, see URL: http://support.microsoft.com/kb/316005) and also on how to extract some info. from the XML but my request is kind of odd since I want to check for the Country first and then get that Counties Rate only and not the other one, I want to do this for the entire xml file.
For example the xml file is something like this:
(or you can view the full xml file by clicking on the URL above)
<FILE>
<QUARTER>2Q2012</QUARTER>
<RECORD>
<JURISDICTION ID="#15">AB</JURISDICTION>
<COUNTRY>CAN</COUNTRY>
......
......
<RATE RATECHANGE="0" COUNTRY="US">0.3366</RATE>
<RATE RATECHANGE="0" COUNTRY="CAN">0.0900</RATE>
......
......
......
</RECORD>
<RECORD>
<JURISDICTION ID="#15">FL</JURISDICTION>
<COUNTRY>U.S.</COUNTRY>
......
......
<RATE RATECHANGE="0" COUNTRY="US">1.5700</RATE>
<RATE RATECHANGE="0" COUNTRY="CAN">1.3210</RATE>
......
......
......
</RECORD>
</FILE>
and so on....
Now I would like to insert that info into the SQL Server table called FFTR and name the column specific for each JURISDICTION
Like for example the above would be:
Field Name 1 --> "JURISDICTION_AB_CAN"
Field Name 2 --> "JURISDICTION_FL_US"
and so on...
NOTE:
The prefix JURISDICTION_ will always be the same only the two letters will change and the CAN can become US.
Another thing is if the COUNTRY is "CAN" then I would like to use the CAN Rate and if it's U.S. I would like to use the US Rate and insert that info. into the database with the Field named "RATE". (The Rate will always be 4 decimal places) the Rate I want is only under: <FUEL_TYPE>Special Diesel</FUEL_TYPE> I don't need the other Rates.
And the last thing I would like to do is to have the <QUARTER>2Q2012</QUARTER> inserted into a Field named "Quarter"
So the final SQL Server database would look like this (using the 2 records as an example above)
Field Name: JURISDICTION_AB_CAN
Rate: 0.0900
Quarter: 2Q2012
Field Name: JURISDICTION_FL_US
Rate: 1.5700
Quarter: 2Q2012
So what I tried to do is this (see code below) and I got it to show each line but it doesn't even come close to a solution:
<%
Option Explicit
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.setProperty "ServerHTTPRequest", True
xml.Load ("http://www.iftach.org/TaxMatrix/charts/2Q2012.xml")
Dim paragraph1,paragraph2,paragraph3,paragraph4,paragraph5,paragraph6
paragraph1 = xml.documentElement.childNodes(1).text
paragraph2 = xml.documentElement.childNodes(2).text
paragraph3 = xml.documentElement.childNodes(3).text
paragraph4 = xml.documentElement.childNodes(4).text
paragraph5 = xml.documentElement.childNodes(5).text
paragraph6 = xml.documentElement.childNodes(6).text
Set xml = Nothing
%>
<html>
<head>
<title></title>
</head>
<body>
<p align="center"><% = paragraph1 %></p>
<p align="center"><% = paragraph2 %></p>
<p align="center"><% = paragraph3 %></p>
<p align="center"><% = paragraph4 %></p>
<p align="center"><% = paragraph5 %></p>
<p align="center"><% = paragraph6 %></p>
</body>
</html>
I even think that adding it to a ADODB Recordset would be great and then I would insert it into SQL Server one by one or just loop it all in there, but it only shows me the columns I need the rows in there also. See code below:
<%
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = "Provider=MSDAOSP; Data Source=MSXML2.DSOControl.3.0;"
objRS.Open(Server.MapPath("2Q2012.xml"))
Response.Write(objRS.Fields(2) & "<br>") ' <-- Returns the Quarter only, that I need for the Quarter Field in the DB
'Response.Write(objRS.Fields(6) & "<br>") ' <-- Returns the entire xml page
Do While Not objRS.EOF
objRS.MoveNext
Loop
%>
<table border="1" width="100%">
<%
dim fld
Response.Write("<tr>")
For Each fld in objRS.Fields
If fld.Name <> "$Text" Then
Response.Write("<td>" & fld.Name & "</td>")
End If
Next
Response.Write("</tr>")
Do While Not objRS.EOF
Response.Write("<tr>")
For Each fld in objRS.Fields
If fld.Name <> "$Text" Then
Response.Write("<td>" & fld.Value & "</td>")
End If
Next
Response.Write("</tr>")
objRS.MoveNext
Loop
%>
</table>
Again, Thank you so much for any advice, links, or any help at all...
have a look here: msxml DOM
you should use the msxml object to read the xml. then you can query the elements of the xml by using the api.
example code for loading the xml:
<%
dim xml : set xml = server.createobject("Msxml2.DOMDocument.6.0")
dim xmlString : xmlString = getXMLHTTPResponse("http://www.iftach.org/TaxMatrix/charts/2Q2012.xml")
xml.loadxml(xmlString)
function getXMLHTTPResponse(url)
dim tout, xmlhttp
set xmlhttp = server.createObject("Msxml2.ServerXMLHTTP.6.0")
with xmlhttp
.setTimeouts 2000, 2000, 2000, 2000
.open "GET", url, false
.send()
if .responseXML.xml = "" then
getXMLHTTPResponse = .responseText
else
getXMLHTTPResponse = .responseXML.xml
end if
end with
end function
%>
Finally!!! I made it work, if anyone needs the solution it is below:
Using the code I posted in the question I added the following code:
Note: I know that this is not the best way to do it since I'm not going after the TAGS/IDs/Names but the xml file will always stay formated the same so I just Looped, Cut out what I needed, and Inserted/Updated into the DB.
<%
' LOOP ALL OF THE FIELDS ONE BY ONE
For x = 0 to xml.documentelement.childnodes.length - 1
set XMLobjchildnodes = xml.documentelement.childnodes
strXMLtxt=xml.documentElement.childNodes(x).text & "<br>"
' SETUP THE UPDATE FIELDS FOR SQL
dim strTest
strTest="objSQL(""IMP_STATE_" & Mid(strXMLtxt,1,2) & """)=" & Mid(strXMLtxt,46,7)
' SKIP THE Fi and 2Q because they are not States/Prov.
if strTest="objSQL(""IMP_STATE_Fi"")=" OR strTest="objSQL(""IMP_STATE_2Q"")=" then
else
' ALSO SKIP hi and U and CN because they also are not States/Prov.
if InStr(strTest,"STATE_ht")>0 OR InStr(strTest,"STATE_U.")>0 OR InStr(strTest,"STATE_CN")>0 then
else
' ADD YOUR SQL CONNECTION INFO. HERE AND THEN INSERT/UPDATE THE SQL DB
end if
Next
%>
Note: By making small changes you can set it to Insert the data into SQL and/or update, should this be a problem for anyone please let me know I will be more then happy to help.
To all that have tried to solve my problem/question, Thank you so much for all of your hard work and effort.