Snowflake Datawarehousing Getting the timestamp and use it in Insert stament - insert

I am Trying to get the current_timestamp()::timestamp_ntz in a variable and trying use the variable in the insert statement for a timestamp datatype its giving error and when i debug the format was "Mon Dec 09 2019 04:24:50 GMT-0800 (PST)" this in the return statement of the procedure. When i am trying to insert into a timestamp column using that variable its giving error.how to resolve this.

Try leveraging Snowflake's variables instead. The issue you are seeing is because you are setting the current_timestamp() to a text string and then trying to insert it back in, but JS is changing the format. Do something like:
SET var = current_timestamp();
INSERT INTO XXX(LOAD_END_TS) VALUES ($var);
This will work for the duration of the session. The other thing you could try is to change your INSERT in JS to something like:
var rs = snowflake.execute( { sqlText: "select current_timestamp()::timestamp_ntz::varchar"} );
var my_sql_command = "INSERT INTO XXX(LOAD_END_TS) VALUES('"+end_ts+"'::timestamp_ntz);" ;

Related

update query remove quotes from query in codeigniter

i am using codeigniter with postgresql database
i want to set Default value if we can't get value or value == '' from $_POST or $this->input->post() then need to set value DEFAULT but whenever codeigniter execute query its append a quote like "DEFAULT" but i want my query like below example
Example:
Current Output :
update tablename set fieldname = 'DEFAULT' where
Required Output :
update tablename set fieldname = DEFAULT where condition
here problem with codeigniter query which append quotes for DEFAULT value whenever i will update
please help me to remove quotes from core file whenever i will execute query in database through codeigniter
This is a valid question. First, in postgres for example there are constants such as CURRENT_DATE and CURRENT_TIMESTAMP or in MySQL NOW() that, if quoted will cause an error. The way I handle these case is by using the code igniighter set function with a third parameter set to False to tell code ignighter to False like so:
$this->db->where('id',999);
$this->db->set('draft', False);
$this->db->set('submission_date', 'CURRENT_DATE', False);
$this->db->set('status','ok');
$this->db->update('projects');
This will produce the following query:
UPDATE projects SET draft=False, submission_date=CURRENT_DATE, status='ok' WHERE id='999';
Reference: https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data

Extract only date part in a datetimein LINQ

We have some data in a DataTable and we are using the query like this to get what we need.
IEnumerable<Results> subResult = from query in datatable.AsEnumerable()
select new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date")
}
This above query returns what i need but date in full format (Ex: m/dd/yyyy hh:min:sec am/pm) but we need only date part of it(only mm/dd/yyyy need to be pulled in). When looked in the properties of this, couldn't find an implicit way to get it, please help me in getting this result. Thanks.
The DateTime class has all the properties you just mentioned. You should try to fix your display with the ToString("anyformatyouwant") formats.
What about this?
Date = query.Field<DateTime?>("Date").Date
This will give you just the date part:
IEnumerable<Results> sub-result= from query in datatable.AsEnumerable()
where new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date").Date
}
However if you just want to display the date somewhere (meaning you're not using this to do date grouping, etc. then I would just specify the display format in your UI layer.
AFAIK there is no Date Class in .net, if you want pure date you should write a Class for that, otherwise you can pull out the part as a string
DateTime.Now.Date.ToString("yyyy/MM/dd")
gives you the current date in string type, but if you get the .Date part it gives you a DateTime object which still has a time part showing AM 12:00:00:0 blah blah

How do I execute a sql statement in lotusscript if the table has a timestamp field/column?

I'm working to retrieve data from oracle 9i to lotus notes. I'm using Oracle 10g for testing since that's what I had. In the default HR database, there is an EMPLOYEES table. I've recently added a new column to get the last modified timestamp which is successful. The data is in the form like this: 25-JUL-12 10.28.32.000000 AM
The following is my lotusscript code:
Option Public
Option Declare
UseLSX "*lsxlc"
%Include "lsconst.lss"
Sub Initialize
Dim s As New NotesSession, db As NotesDatabase
Set db=s.Currentdatabase
Dim lcs As New Lcsession
lcs.Clearstatus
Dim conUser As New Lcconnection("Oracle")
Dim staffdoc As NotesDocument
Dim fieldlistuser$
fieldlistuser="EMPLOYEE_ID,FIRST_NAME,MODIFIED_AT"
conUser.Server="localhost"
conUser.UserID="hr"
conUser.Password="hr"
conUser.Connect
conUser.MetaData="HR.EMPLOYEES"
conUser.Fieldnames=fieldlistuser
Dim fieldsUser As New LCFieldList
Dim fieldUser As LCField
Call conUser.Execute("Select * From HR.EMPLOYEES", fieldsUser)
While conUser.Fetch(fieldsUser) > 0
Set staffdoc=New NotesDocument(db)
staffdoc.Form="Staff"
staffdoc.StaffID=fieldsUser.EMPLOYEE_ID(0)
staffdoc.StaffName=fieldsUser.FIRST_NAME(0)
staffdoc.DateJoin=fieldsUser.MODIFIED_AT(0)
Call staffdoc.Save(True, True)
Wend
conUser.Disconnect
End Sub
Previously everything was ok before I added the timestamp column in the EMPLOYEES table and I can export every row to a temporary lotus view. Now the error always stop at Call conUser.Execute("Select * From HR.EMPLOYEES", fieldsUser). I thought it has something to do with the line fieldlistuser="EMPLOYEE_ID,FIRST_NAME,MODIFIED_AT" so I remove MODIFIED_AT from it and commented staffdoc.DateJoin=fieldsUser.MODIFIED_AT(0) but the error still occurs. The error is Error: Invalid data type for field 'MODIFIED_AT', Connector'Oracle', Method -Execute-. Can this actually be done at all? If can, what datatype in lotus should I store the timestamp value?
My first thought is does the select statement work fine at the server?
Assuming it does, this looks like a problem with the driver converting the timestamp to a Lotus Notes datetime field. As a workaround you could create a stored procedure that returns 'modified at' as a datetime instead of a timestamp type.
Just looking at help docs and I'm wondering if captialization matters, as the LCConnection property I see listed is FieldNames, not Fieldnames.

Accessing MySQL built-in functions using Codeigniter

If I have a MySQL statement like this:
$sql="SELECT `name`, DATE(`time`), FROM `students` WHERE `id`='5'";
I am using CodeIgniter and trying to fetch the result in object format rather than array format (i.e. not using row_array())
$query=$db1->query($sql);
$row=$query->row();
I can get the name as
echo $row->name;
I cannot get the value of date with this method.
Any Ideas how I can get the date with this method, with something like:
echo $row->date;
?
You should be able to use your own field names in SQL like so:
$sql="SELECT `name`, DATE(`time`) as mydate, FROM `students` WHERE `id`='5'";
and subsequently access it like
echo $row->mydate;
If you want the column in the result set to be named date, then add AS date after the DATE expression. You'll then be able to access it as $row->date.

Extracting timestamp field in Oracle vs MySQL from Grails-Groovy

I am using grails/groovy, and from my controller I am currently doing this for retrieving field from Mysql table containing datetime field
SimpleDateFormat Sformat = new SimpleDateFormat("yyyy-MM-dd");
String format_datenow = Sformat.format(new Date());
String format_dateprevious = Sformat.format(new Date() -31);
String markerCalcQuery =
"select sum(trans_cnt) as t_cnt, location from map2_data where fdate between '"+format_dateprevious+"' and '"+format_dateprevious+"' and res_id = "+res_id+" group by map2_data.location";
res_row=gurculsql.rows(markerCalcQuery);
The above query fails on Oracle11g with error
ORA-01843: not a valid month.
The error I feel is because MySQL stores date in this format: 2011-12-28 02:58:26 and Oracle stores date like this: 28-DEC-11 02.58.26.455000000 PM
How do I make the code generalised, one way is to make the database in Oracle store the date in the same format which I am thinking the way to handle this rather than from the code. If yes, how to change date format in the Oracle db?
Can I specify the format in the grails domain class for map2_data so that no matter what database it is we will have the datetime in the same format.
For several reasons (one being to code database independent - which is basically what you'd need ;-)), it is better to avoid creating SQL statements in your code. Try to use the Grails criteria DSL, e.g. something like
def criteria = YourDomainObject.createCriteria()
criteria.get {
between ('fdate', new Date()-31, new Date())
projections {
sum('trans_cnt')
groupProperty('location')
}
}
(ontested, but should help you get started).
If for some reason you can't use the criteria API, try the fallback to HQL (Hibernate Query Language). I'd always try to avoid to write plain SQL.
In Oracle, dates have their own type, they aren't strings. If you have a string, you should convert it to a date using the TO_DATE function.
String format_datenow = "TO_DATE('" + Sformat.format(new Date()) + "', 'YYYY-MM-DD')";
To make it work also in MySQL, you can create a stored function named TO_DATE that just returns its first argument.

Resources