How to convert string with a specific date format like 'mm/dd/yy' to date object in hibernate query - sql

One of the columns my_column in my table is of type varchar where i store value of date in string. The format is 'MM/DD/YY'. I want to use cast function of hibernate to convert this column value to date. But it appears that the cast function accepts string of 'yy-mm-dd' format to convert. Is it possible to use cast for string with specific date format for conversion. Something like this: cast(am.my_column as date format 'mm/dd/yy')
cast function in hibernate is used this way: cast(am.my_column as date).
My use case is basically converting the column value from string to date and making a date comparison. cast(am.my_column as date format 'mm/dd/yy') > '12/30/2017'

Instead of casting a column value inside the query, you may consider to give '12/30/2017' as parameter (with type of Java Date). So that you can directly write am.my_column > :parameterDate

Related

casting a date field to a timestamp in Databricks SQL

I have a source table in databricks which has a field called date. I found that the date is like a timestamp. For example, it's data is date = 2021-07-09 00:07 , date = 2021-12-21 09:19.
The above date is not a full timestamp, so I casted it as a string and I got the following results
cast(date as string) = 2021-07-09 00:07:09.529, 2021-12-21 09:19:21.019
I would like to know why casting it to a string is giving the milli seconds and is there any to_timestamp like function to cast the date instead of casting as string.
Can the date casted as string be compared with other dates casted as string?
if the date field has timestamp, then what you see is just visual representation that may omit some non-essential details - internally it's stored in more compact representation, like, long representing number of milliseconds. But when you do cast to string, then this compact representation is used & you get all details.
To cast timestamp type to date type, just do cast(date as date). If you want to convert string into date or timestamp, just use to_date or to_timestamp functions that allow to specify a format string that will be used for parsing of string components. And if you want to convert date or timestamp field into string and control the format of the string, then you need to look onto date_format function.
P.S. You can always refer to the SQL Reference that is the part of official Databricks documentation

Date casting in oracle

How does following query work , could not find documents explaining how following query works.Are there any other similar queries for casting values?
SELECT date '2017-01-01' FROM dual;
Gives result 1/1/2017
Your query is using a date literal. From the Oracle documentation:
You can specify a DATE value as a string literal, or you can convert a character or numeric value to a date value with the TO_DATE function. DATE literals are the only case in which Oracle Database accepts a TO_DATE expression in place of a string literal.
To specify a DATE value as a literal, you must use the Gregorian calendar. You can specify an ANSI literal, as shown in this example:
DATE '1998-12-25'

SQL Date Format Conversion

I have a question regarding SQL dates.
The table I am working with has a date field in the following format: "22-SEP-08". The field is a date column.
I am trying to figure out how to output records from 1/1/2000 to present day.
The code below is not filtering the date field:
Select distinct entity.lt_date
from feed.entitytable entity
where entity.lt_date >= '2000-01-01'
Any help regarding this issue is much appreciated. Thanks!
Edit: I am using Oracle SQL Developer to write my code.
DATEs do not have "a format". Any format you see is applied by the application displaying the date value.
You can either change the configuration of SQL Developer to display dates in a different format, or you can use to_char() to format the date the way you want.
The reason your statement does not work, is most probably because of the implicit data type conversion that you are relying on.
'2000-01-01' is a string value, not a date. And the string is converted using the NLS settings of your session. Given the fact that you see dates displayed as DD-MON-YY means that that is the format that is used by the evil implicit data type conversion. You should supply date values always as real date literals.
There are two ways of specifying a real date literal. The first is ANSI SQL and simple uses the keyword DATE in front of an ISO formatted string:
where entity.lt_date >= DATE '2000-01-01'
Note the DATE keyword in front of the string, wich makes it a real date literal not a string expression.
The other option is to use to_date() to convert a character value into a date:
where entity.lt_date >= to_date('2000-01-01', 'yyyy-mm-dd');
More details about specifying date literals can be found in the manual:
Date literals
to_date function
My guess is the data type isn't a Date. Just in case its a char type, try to convert it using the Oracle TO_DATE() function. The Oracle documentation below should help you with parameters.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm
An implicit datatype conversion bites once again.
You're right. The predicate is not doing the comparison you are expecting,
Oracle is performing an implicit datatype conversion, from DATE to VARCHAR, so that it can do a comparison to the string literal.
If lt_date column is DATE datatype, then Oracle is seeing your where clause:
where entity.lt_date >= '2000-01-01'
Oracle is actually seeing it as if it's written like this:
where TO_CHAR(entity.lt_date) >= '2000-01-01'
And that's where the "format" problem comes in. The column itself does not have a "format". Because the second argument to the TO_CHAR function is not supplied, Oracle is using the value of the NLS_DATE_FORMAT parameter (from your session). And that's probably set to DD-MON-YY. Which is why that's the "format" you're seeing when you a run a SELECT statement in SQL*Plus. Because the DATE value is (again) being run through a TO_CHAR function to get a string that can be displayed.
To get the "filtering" you want, don't do a comparison to a string literal. Instead, do the comparison to an expression that has DATE datatype.
You can use the Oracle TO_DATE function. And you don't want to rely on setting of NLS_DATE_FORMAT, explicitly specify the format model as the second argument to the function. For example:
DO THIS
where entity.lt_date >= TO_DATE('2000-01-01','YYYY-MM-DD')
DON'T DO THIS
It's also possible to specify the format model as the second argument to the TO_CHAR function.
where TO_CHAR(entity.lt_date,'YYYY-MM-DD') >= '2001-01-01'
But you don't want to do that because that's going to force Oracle to evaluate that expression on the left side for every flipping row in the table, so it has a string value to do the comparison. (That's true unless someone created a function-based index for you.) If you do the comparison on the bare column, using the TO_DATE on the literal side, Oracle can make effective use of an appropriate index (with lt_date as the leading column) to satisfy the predicate.

Converting Date format in Sql Server

I have a column in my table for storing dates and it is in 12-06-2013 15:32:45. I want to convert it to MM/DD/YYYY format. How can I do it?
Coulmn type is varchar
First you need to CONVERT VARCHAR() to datetime type and and then to CONVERT it to string in desired format:
SELECT CONVERT (varchar (10), CONVERT (date,'12-06-2013 15:32:45' ,103),101)
First 103 is used to interpret current date format, and second - 101 - target format.
If you change target date format from varchar to date then your output in MSMS will be in default display date format of you SQL Server, not the desired format. This is because SQL Server stores dates as integers and converts them before dispalying the value. Therefore if you need to store in certain format, then store in VARCHAR type.
Check out CAST and CONVERT functions on MDSN
or you can do it in your c# code like that :
First you have to take your date from your table and put it in string var "DT" exemple :
string strDT =db.TableTitle.date;
string date = strDT .ToString("MM/DD/YYYY");
DateTime DT = Convert.ToDateTime(date);
then you can use the DT time variable :)

How can I convert a varchar field (YYYYMM) to a date (MM/01/YY) in SQL?

I'm sure this is quite simple, but I've been stuck on it for some time. How can I convert a varchar field (YYYYMM) to a date (MM/01/YY) in SQL?
Thanks.
Edit: I'm using Open Office Base (HSQL), not MySQL; sorry for the confusion.
Try the str_to_date and date_format functions. Something like:
select date_format( str_to_date( my_column, '%Y%c' ), '%c/01/%y' ) from my_table
try :
SELECT STR_TO_DATE(CONCAT(myDate,'01'),'%Y%m%d')
FROM myTable
Use STR_TO_DATE:
From mysql.com:
STR_TO_DATE(str,format)
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
The date, time, or datetime values contained in str should be given in the format indicated by format. For the specifiers that can be used in format, see the DATE_FORMAT() function description. If str contains an illegal date, time, or datetime value, STR_TO_DATE() returns NULL. Starting from MySQL 5.0.3, an illegal value also produces a warning.
Range checking on the parts of date values is as described in Section 11.3.1, “The DATETIME, DATE, and TIMESTAMP Types”. This means, for example, that “zero” dates or dates with part values of 0 are allowed unless the SQL mode is set to disallow such values.
mysql> SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');
-> '0000-00-00'
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
Get the year:
SUBSTRING(field FROM 2 FOR 2)
Get the month:
SUBSTRING(field FROM -2 FOR 2)
Compose the date:
CONCAT(SUBSTRING(field FROM -2 FOR 2), '/01/', SUBSTRING(field FROM 2 FOR 2))
This will convert from YYYYMM to MM/01/YY.
To be clear: if you're looking for method to convert some value of type Varchar/Text to value of type Date than solutions are:
using CAST function
CAST(LEFT('201205',4)||'-'||SUBSTRING('201205' FROM 5 FOR 6)||'-01' AS DATE)
starting from OpenOffice 3.4 (HSQLDB 2.x) new Oracle-like function TO_DATE supposed to be available
TO_DATE('201205','YYYYMM')
in addition to the written i can mention that you also can construct a string with ANSI/ISO 'YYYY-MM-DD' formatted representation of the date,- Base will acknowledge that and succesfully convert it to the Date type if necessary (e.g. INSERTing in Date typed column etc.)
Here is doc's on HyperSQL and highly recommended OO Base guide by Andrew Pitonyak