DAte Format in Sybase - sap-ase

How can I convert a column of datetime data which stores value like this by default in Sybase
Sep 15 1999 12:23:56:230AM
to this format:
MM/DD/YY HH:MI:SS,FF9
I don't see any option for convert function

Do it yourself:
declare #test datetime
select #test = getdate()
select convert(varchar, #test, 1) + ' ' + convert(varchar, #test, 108) + ',FF9'

Related

Converting STRING to DATE in BigQuery

I have a column called order_date in a table in BigQuery and it is of the data type STRING. It has two types of values 10/2/2020 and 2020-10-02. PARSE_DATE('%m/%d/%Y', order_date) converts 10/2/2020 to DATE and CAST(order_date as DATE) converts 2020-10-02 to DATE. I wrote the CASE statement below to handle both of the cases but it is erroring out.
CASE
WHEN FORMAT(order_date) ='%m/%d/%Y'THEN PARSE_DATE('%m/%d/%Y',order_date)
ELSE CAST (order_date AS DATE) END AS order_date
You could use a regex pattern to assert the %m/%d/%Y variant:
SELECT
CASE WHEN REGEXP_CONTAINS(order_date, r"^\d{2}/\d{2}/\d{4}$")
THEN PARSE_DATE('%m/%d/%Y', order_date)
ELSE CAST(order_date AS DATE) END AS order_date
FROM yourTable;

Converting string to date and using query greater than operator is not working as expected in cassandra

I am using cassandra 2.2.3 and below is my table structure to store doj as date object.
Table structure:
class Emp{
String name
Date doj
}
empdata:
id name doj
1 Test 2010-01-01 00:00:00+0000
2 Test1 2011-01-01 00:00:00+0000
I have to use greater than or less than operator to select the employees between those dates.
From the json i get the following input to display the data.
{
"dateFrom":'1/1/2010'
}
//Modified the string to date using SimpleDateFormat
Date d1 = f.parse(dateFrom);
long milliseconds = d1.getTime();
//Query call to use the gte operator to fetch the data from Cassandra
Select selectQuery = QueryBuilder.select().all().from(tableName).allowFiltering()
Where selectWhere = selectQuery.where();
rangeClause(selectWhere,dateFrom,Tue Jan 01 00:00:00 PST 2010);
Statement s = selectWhere.limit(1)
println("st is:"+s);
In the select statement i see the query as "SELECT * FROM emp WHERE doj>=1551427200000 LIMIT 1;
//Function Call
def rangeClause(Where selectWhere, String columnName, Object columnValue)
{
Clause whereClause = null
whereClause= QueryBuilder.gte(columnName, columnValue.get("dateFrom"))
selectWhere.and(whereClause)
}
dateFrom is a timestamp (long) which is 8 bytes and your passing it a String '2010-01-05' thats 10 bytes. You need to convert that date string into a epoc timestamp (long) or Date.

Excel converts DateTime to Int

I'm importing table from SQL Server to Excel by VBA.
Here is the part of code
For Each fld In rs.Fields
Sheet1.Cells(row, col).NumberFormat = "#"
Sheet1.Cells(row, col).Value = fld
col = col + 1
Next
I have field in SQL Server which is DateTime, but it is imported to Excel as int (some weird number appears). I've specified explicitly
Sheet1.Cells(row, col).NumberFormat = "#"
but that didn't help. How I can import DateTime field from SQL Server to Excel either as DateTime or Text
And I want to keep format the same as in SQL Server, which is 2017-11-01 00:00:00.000
I was able to fix it by converting datetime into varchar in query inside VBA
convert(varchar(55),runDate,121)
Excel somehow can't understand DateTime datatype

PostgreSQL : cast string to date DD/MM/YYYY

I'm trying to cast a CHARACTER VARYING column to a DATE but I need a date format like this : DD/MM/YYYY. I use the following SQL query :
ALTER TABLE test
ALTER COLUMN date TYPE DATE using to_date(date, 'DD/MM/YYYY');
The result is a date like this : YYYY-MM-DD.
How can I get the DD/MM/YYYYformat ?
Thanks a lot in advance !
Thomas
A DATE column does not have a format. You cannot specify a format for it.
You can use DateStyle to control how PostgreSQL emits dates, but it's global and a bit limited.
Instead, you should use to_char to format the date when you query it, or format it in the client application. Like:
SELECT to_char("date", 'DD/MM/YYYY') FROM mytable;
e.g.
regress=> SELECT to_char(DATE '2014-04-01', 'DD/MM/YYYY');
to_char
------------
01/04/2014
(1 row)
https://www.postgresql.org/docs/8.4/functions-formatting.html
SELECT to_char(date_field, 'DD/MM/YYYY')
FROM table
The documentation says
The output format of the date/time types can be set to one of the four
styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date
format), or German. The default is the ISO format.
So this particular format can be controlled with postgres date time output, eg:
t=# select now();
now
-------------------------------
2017-11-29 09:15:25.348342+00
(1 row)
t=# set datestyle to DMY, SQL;
SET
t=# select now();
now
-------------------------------
29/11/2017 09:15:31.28477 UTC
(1 row)
t=# select now()::date;
now
------------
29/11/2017
(1 row)
Mind that as #Craig mentioned in his answer, changing datestyle will also (and in first turn) change the way postgres parses date.
In case you need to convert the returned date of a select statement to a specific format you may use the following:
select to_char(DATE (*date_you_want_to_select*)::date, 'DD/MM/YYYY') as "Formated Date"
Depends on which type you require as output, but here are 2 quick examples based on an intervals:
SELECT (now() - interval '15 DAY')::date AS order_date -> 2021-07-29
SELECT to_char(now() - interval '15 DAY', 'YYYY-MM-DD') -> 2021-07-29
Let's say your date column is order_date:
SELECT (
RIGHT(order_date, 4)
|| '-'
|| SUBSTRING(order_date, 4, 2)
|| '-'
|| LEFT(order_date, 2)
)::DATE
FROM test
OR
SELECT CAST(
RIGHT(order_date, 4)
|| '-'
|| SUBSTRING(order_date, 4, 2)
|| '-'
|| LEFT(order_date, 2)
AS DATE )
FROM test

1 = 1 returns False in T-SQL - Why?

Please look at the snippet below
DEClaRE #p__linq__0 datetime
SET #p__linq__0 = '2012-02-01 00:00:00'
SELECT (STR( CAST( DATEPART (day, #p__linq__0) AS float)))
SELECT
InvoicingActivityStartDay,
(STR(CAST( DATEPART (day, #p__linq__0) AS float))),
CASE WHEN STR(CAST(DATEPART (day, #p__linq__0) AS float))= InvoicingActivityStartDay THEN 'EQUAL' ELSE 'NOT EQUAL' END
FROM INVOICEMETADATA
This was the rough SQL Translation of a Linq-to-Entities query I had in my application. The two possible values for InvoicingActivityStartDay are 1 and 20.
This snippet results in rows like this:
InvoicingActivityStartDay Column1 Column2
1 1 NOT EQUAL
20 1 NOT EQUAL
I understand why it returns NOT EQUAL for the second row; but why does it return NOT EQUAL for the first row where 1 = 1?
Is InvoicingActivityStartDay a string? SELECT STR(CAST(DATEPART (day, getdate()) AS float)) returns a string. Are you expecting an integer comparison?

Resources