To_date function in Vertica format? - database

How can I get Vertica function to_date('','format') do output like this:
DDMonYYYY - 01/ABR/2012
and not like it does (01-04-2012)?!
dbadmin=> select now();
now
--------------------------------
19/09/2012 11:03:48.284339 BRT
(1 row)
dbadmin=> show datestyle;
name | setting
-----------+----------
datestyle | SQL, DMY
(1 row)

Try this:
SELECT TO_CHAR(CURRENT_TIMESTAMP, 'DD/MON/YYYY');

to_date() takes a string and converts it into a date object. The output you see returned is not formatted to the specified format. It is just an acknowledgement that the date object got created.

Related

How to convert regexp_substr(Oracle) to SQL Server?

I have a data table which has a column as Acctno what is expected shows in separate column
|Acctno | expected_output|
|ABC:BKS:1023049101 | 1023049101 |
|ABC:UWR:19048234582 | 19048234582 |
|ABC:UEW:1039481843 | 1039481843 |
I know in Oracle SQL which I used the below
select regexp_substr(acctno,'[^:]',1,3) as expected_output
from temp_mytable
but in Microsoft SQL Server I am getting an error that regexp_substr is not a built in function
How can I resolve this issue?
We can use PATINDEX with SUBSTRING here:
SELECT SUBSTRING(acctno, PATINDEX('%:[0-9]%', acctno) + 1, LEN(acctno)) AS expected_output
FROM temp_mytable;
Demo
Note that this answer assumes that the third component would always start with a digit, and that the first two components would not have any digits. If this were not true, then we would have to do more work.
Just another option if the desired value is the last portion of the string and there are not more than 4 segments.
Select *
,NewValue = parsename(replace(Acctno,':','.'),1)
from YourTable

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

How to convert a value of date type to text in Cassandra?

I have a Cassandra table with following structure -
CREATE TABLE timeseries_s (
prop_name text PRIMARY KEY,
description text,
value text
);
Now I need to insert a property in this table whose value will be current date in text format. I have created the following CQL, but it gives me the following error -
INSERT INTO timeseries_s (prop_name, description, value) VALUES ('xyz', 'abc', TODATE(now())));
Error - SyntaxException: line 1:168 mismatched input ')' expecting EOF (...'abc', TODATE(now()))[)]...)
Thereafter, I tried below -
INSERT INTO timeseries_s (prop_name, description, value) VALUES ('xyz', 'Migration for DSE started at this time', CAST(TODATE(now()) AS TEXT));
Error - SyntaxException: line 1:158 no viable alternative at input '(' (... VALUES ('xyz', 'abc', CAST
Any suggestions?
The only way I can think of to solve this one purely on the Cassandra side, is with a user defined function. First of all, you'll need to enable user defined functions in the cassandra.yaml:
enable_user_defined_functions: true
Once the node(s) has been restarted, I accomplished this by defining a function called totext in my stackoverflow keyspace, like this:
aploetz#cqlsh:stackoverflow> CREATE OR REPLACE FUNCTION totext (input DATE)
RETURNS NULL ON NULL INPUT RETURNS TEXT
LANGUAGE java AS 'return input.toString();';
With that function created, you can use it in your INSERT like this:
> INSERT INTO timeseries_s (prop_name, description, value)
VALUES ('xyz', 'Migration for DSE started at this time',
stackoverflow.totext(todate(now())));
> SELECT * FROM timeseries_s ;
prop_name | description | value
-----------+----------------------------------------+------------
xyz | Migration for DSE started at this time | 2020-09-03
(1 rows)
TODATE takes column_name as input. Also, CAST is used in SELECT statements only. Probably you can achieve this thing through program.

year coming wrong in TO_DATE function in db2

I tried to run below query in DB2 database:
My date string: 122887 mmddyy
select DATE(TO_DATE('122887', 'mmddyy')) from SYSIBM.dual;
now result is: 2087-12-28
But i am expecting below 1987-12-28.
How to achieve this?
You need to use the "adjusted year" for your query. Instead of YY it is RR:
values(DATE(TO_DATE('122887', 'mmddrr')))"
1
----------
12/28/1987
Details are in the documentation for TO_DATE/TIMESTAMP_FORMAT.

Importing Dates into R from SQL Server

I am trying to query a SQL Server database to check for the MAX date in one field and to select the next day for input into another process. I am able to query the database and pull back a date, but I can't convert it into a date object.
Here is what I tried:
tmpMaxDate <- sqlQuery(dbhandle, 'select MAX([date]) + 1 from dbo.My_Data ')
tmpMaxDate
1 2016-01-02
IsDate(tmpMaxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate)
Error in as.Date.default(tmpMaxDate) :
do not know how to convert 'tmpMaxDate' to class “Date”
maxDate
NULL
IsDate(maxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate, format = "%Y-%M-%D")
Error in as.Date.default(tmpMaxDate, format = "%Y-%M-%D") :
do not know how to convert 'tmpMaxDate' to class “Date”
IsDate(maxDate)
[1] FALSE
maxDate
NULL
The packages I am using are RODBC, chron, lubridate, and RSQLserver
I thought I needed to convert to a date object to use the date from SQL Server in R.
Any thoughts on why my date won't convert? Do I need to convert to be able to use that date to filter data in R?
DateVar <- sqlQuery(dbhandle,"SELECT CONVERT(varchar(10),GETDATE() + 1,101) as Date")
DateVar
NewDate <- as.Date(DateVar$Date, format = "%m/%d/%y")
NewDate
I think the trick for me to was give the date response a column name and then reference the column (being the specific string value needed to convert) name in the conversion.
class(NewDate)
will show as "Date"

Resources