Sybase How to get dash separated date yyyy-mm-dd? - datetime

I want to get date in such format yyyy-mm-dd, for example 2014-04-11. But it seems there is no way to do this in Sybase (ASE 12.5) with the convert function.
Currently, I get the date by 112 and add the - between digits. Any good way?

Take advantage of format 140: yyyy-mm-dd hh:mm:ss.ssssss
Use char(10) to make Sybase truncate the string to just the first 10 characters, i.e.
convert(char(10), col1, 140)

Try this:
select str_replace( convert( varchar, col1, 111 ), '/', '-')
from table

Look the table documentation shared by Doberon, the table have all the formats. I try it and works nice:
SELECT convert(char(10),dateadd(month,-1, convert(date,getdate())),112) from table;
My query format is yyyymmdd.

Related

Using a query with a datetime cell in spreadsheets

I need to query a cell that contains a datetime but its formatted by default when I import it to only show the date.
31/7/2020 19:18:58 (in reality it's like this)
31/7/2020 (but it shows this)
So when I run this query:
=QUERY(A5:R10, "select K")
It returns only the date no matter what I do:
31/07/2020
I've tried:
Options
Formatted like "yyyy-MM-dd HH:mm" it returns 00:00
Filter by datetime or timestamp
Used '"&Text(now(), "yyyy-mm-dd HH:mm"&"' or something like that
The question is:
Is there a way to do what I'm trying to do without reformatting the imported cells?
link to test it:
https://docs.google.com/spreadsheets/d/14rGPngGvFXGP8txMS2yFo1v4gPcI4K1oYWj_8yt2Uq8/edit?usp=sharing
when I select one cell with F2 it shows the time:
Thanks a lot for your time!
select column B and format it as date time
or without reformating it:
=ARRAYFORMULA(QUERY(1*(B2:B4&""), "format Col1 'yyyy-mm-dd hh:mm:ss'"))
or:
=ARRAYFORMULA(QUERY(TEXT(B2:B4, "yyy-mm-dd hh:mm:ss"), "select *"))
Just make sure your cells are formatted as Automatic
If not, even if you use the format clause of the query, the clause will be ignored

Issue with Casting string to date in Teradata

I have a string column - COL1 in TABLE1 which is if string data type. This table is loaded by Informatica session ( data coming from mainframe) and the format of the COL1 is YYYY-MM-DD. Now I have to use TABLE1 as the source in my next mapping . In the SQL override query of second mapping i will be casting COL1 to date using the below query .
SELECT
CAST(COL1 AS DATE FORMAT 'YYYY-MM-DD') AS CHK_DT FROM TABLE1
But when i try to execute this query in Teradata SQLA, just to check if it runs fine it gives me below error.
SELECT Failed. 2666: Invalid date supplied for COL1.
Can you please help me resolve this issue ? This is not the only date column which has issue, there are two more date columns . I guess the resolution is same for all three columns .
P.S - Just to verify, I updated all rows of COL1 of TABLE1 as 2016-12-12 and ran the select statement, select worked fine . I then updated COL1 of all rows as 2016-13-12, it gave same error . If either of DD or MM is more than 12, it is giving me error
Thanks
If DATE is represented/stored in ANSI standard literal YYYY-MM-DD, the CAST will work.
SELECT CAST('2016-12-13' AS DATE FORMAT 'YYYY-MM-DD') AS Date1
However i doubt that in your case.
The date is most probably in YYYY-DD-MM format. In that case the ANSI standard format will throw the error. You need YYYY-DD-MM
select CAST('2016-13-12' AS DATE FORMAT 'YYYY-DD-MM') AS Date2
P.S. You can confirm the conversion to date using TYPE() function. It should return DATE in your case
Hi Please try this piece of code
CAST(CAST(date_col AS FORMAT 'YYYY-MM-DD') AS VARCHAR(15))
instead of the transformation you are using.
Thanks for your response. However the issue was something else. Some of the incoming records had space in this column . So I had to tweak my informatica mapping to put a trim on date column . Now the select is running fine . Thanks for your time .

Sqlite date between

can you please help me why this code does not work?
I dont understand why the result include "2017".
SQLLITE
QUERY
SELECT issue_date as count FROM tablename where issue_date >= "08/08/2016" and issue_date < "09/01/2016"
Result
"08/08/2017"
"08/11/2017"
"08/18/2017"
"08/18/2017"
"08/22/2017"
"08/22/2017"
"08/28/2017"
"08/31/2017"
Create query
CREATE TABLE tablename (
issue_date datetime text not null
}
Insert query
INSERT INTO tablename (issue_date) values ("08/31/2017");
You are storing your dates in a non ANSI compliant format. As there is no formal date type in SQLite, and all dates are essentially stored as strings, your current date comparison will behave and sort as if you are comparing to text. It won't work, because you have the month first, followed by the day, followed by the year. To get text comparisons of dates to work correctly, use a format something like this:
yyyy-mm-dd
You should change the format you use to store dates, but one workaround would be to build the issue date in the correct format and then do the comparison, also against a date string in the same correct format:
SELECT
issue_date AS count
FROM tablename
WHERE
SUBSTR(issue_date, 7, 4) || '-' ||
SUBSTR(issue_date, 1, 2) || '-' ||
SUBSTR(issue_date, 4, 2) BETWEEN '2016-08-08' AND '2016-09-01'
SQLite's data types does not have a real DATETIME type. It only has NULL, INTEGER, REAL, TEXT and BLOB. Any other type is converted to these, so what it is doing in your case is storing those values as strings, and this comparing as strings.
I personally prefer to store the dates as UNIX timestamps (hence integers) to avoid complicating the SQL queries and simplify the whole thing (although the values in database will become less human-readable).
Because you specified the query with AND. So it matches your query. Use BETWEEN expression.
expression BETWEEN value1 AND value2;

DB2 Cast DateTime string with "T" separator

Help me please with the next problem.
I have date time string in the next format(ISO 8601): 1999-12-31T23:59:59
and I need to cast it to TIMESTAMP value. The main problem in 'T' separator character.
I have tried next query:
SELECT TIMESTAMP_FORMAT('1999-12-31T23:59:59','YYYY-MM-DD HH24:MI:SS') FROM ROMAN.EMPLOYEE;
and use different format strings, such as, YYYY-MM-DDTHH24:MI:SS, YYYY-MM-DD"T"HH24:MI:SS.
Could you provide me correct way to cast this type of strings without any character replacement and substrings.
Thanks in advance!
There is no built-in function that can format a timestamp in ISO-8601 format in DB2 for Linux/UNIX/Windows.
As you have probably surmised, you can do this with REPLACE:
select
TIMESTAMP_FORMAT(REPLACE('1999-12-31T23:59:59','T',' '), 'YYYY-MM-DD HH24:MI:SS')
from
ROMAN.EMPLOYEE;
It's trivial to create a user defined function (UDF) to handle this formatting for you as well so you don't have to out this long string in every query.
It may also be possible to do it via XQuery with and xs:dateTime, although this would be even more code than just embedding REPLACE in the call to TIMESTAMP_FORMAT.

SQLite Extract Month From Column

I have a column of data with this date format mm/dd/yyyy. I would like to covert to this to the format yyyy-mm-dd for (SQLite usage). How could I achieve this?
UPDATE Table_Name
SET Column_Name = 'yyyy-mm-dd' format
I have tried substr, ltrim, rtrim (None of this work for my case).
My data are dynamic.
Sample
Column_Name
6/1/2004
06/25/2004
7/1/2003
6/1/2004
6/1/2004
09/19/2003
09/30/2003
09/30/2003
09/30/2003
09/30/2003
The Goal: Extract only month from this Column (Without displaying unnecessary stuff)
Thank you.
The syntax for extracting a specific date element from a date field is to use strftime.
SELECT strftime('%m', Column_Name) AS month which is your second question. For reconstituting the date data you could use (at least in JavaScript) substr to split the date out then format it as you require. Also see this post:Get month from DATETIME in sqlite

Resources