I am attempting to move a stored procedure from Microsoft SQL Server 2000 to Informix 11. The original SP contains a final select statement with a GROUP BY statement that includes a converted datetime:
group by convert(varchar(8), c.startDateTime, 1)
When I convert this to Informix syntax I get a syntax error at run time:
GROUP BY (c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10)
Can anyone please point me to how, if possible, this can be done in Informix? If this is not possible, which I suspect, how would you typically handle this in the overall query?
I think you need to convert from this:
SELECT a, b, c
FROM ...
GROUP BY (c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10) ;
to something like:
SELECT a, b, c,
(c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10) AS d
FROM ...
GROUP BY 4 ; --- meaning: the 4th column in the SELECT clause
In Informix, it is generally not required to convert datetime fields to character strings to manipulate them. Instead, use the extend function.
Example, if c.StartDateTime is defined as a datetime year to second e.g. 2012-10-28 23:00:00 and you want just the date portion, use extend (c.StartDateTime, year to day). This will return 2012-10-28.
Are you converting to Informix syntax for dates to varchar in mm/dd/yyyy (mssql style=1) as
TO_CHAR(c.StartDateTime,"%m/%d/%iY")
???
Related
This question already has answers here:
Equals(=) vs. LIKE for date data type
(3 answers)
Closed 5 years ago.
Query 1 :
select count(*) from CI_TXN_HEADER where TXN_HEADER_DTTM = '25-JAN-13';
Result: 1
Query 2 :
select count(*) from CI_TXN_HEADER where TXN_HEADER_DTTM like '25-JAN-13';
Result: 19
In my DB I have 19 rows with TXN_HEADER_DTTM as 25-JAN-13.
Data Type of TXN_HEADER_DTTM is DATE.
Can someone please explain the difference in output?
An Oracle DATE column contains a date and a time. The LIKE condition is only for VARCHAR columns. If applied to other data types Oracle implicitly converts that to a varchar (using rules depending on the current client settings).
So you might have rows with e.g. 2013-01-25 17:42:01, however the string constant '25-JAN-13' is (most probably) converted to: 2013-01-25 00:00:00 and thus the = comparison doesn't work.
To find all rows for a specific day use trunc() and a proper date literal. Don't rely on the evil implicit data type conversion to specify date values.
Use trunc() to set the time part of a DATE value to 00:00:00:
I prefer ANSI SQL date literals:
select count(*)
from CI_TXN_HEADER
where trunc(TXN_HEADER_DTTM) = DATE '2013-01-25';
You can also use Oracle's to_date:
select count(*)
from CI_TXN_HEADER
where trunc(TXN_HEADER_DTTM) = to_date('2013-01-25', 'yyyy-mm-dd');
Note that Oracle can't use an index on TXN_HEADER_DTT, so if performance is critical use a range query:
select count(*)
from CI_TXN_HEADER
where TXN_HEADER_DTTM >= DATE '2013-01-25'
and TXN_HEADER_DTTM < DATE '2013-01-25' + 1;
The difference between like and equal is explained in this link very good
https://stackoverflow.com/a/2336940/4506285
I checked your problem on my table and I get the same results.
This link helps also to understand how to compare dates in sql
https://stackoverflow.com/a/18505739/4506285
Maybe your data consists of space, it is not exactly '25-JAN-13' but ' 25-JAN-13';
Please refer this two link:
Equals(=) vs. LIKE
What's the difference between "LIKE" and "=" in SQL?
I have different numbers looking like 40825 and I want to convert them to an actual date in Oracle SQL.
I know it should be SELECT TO_DATE(40825 ,'MM-DD-YYYY') in SQL Server, but this does not work with the same syntax in oracle SQL.
Any help?
If this number mean 4 day, 8 month, and year 2025 then, u must use to_date function with string (not nubmer) and string must looks like date mask.
SELECT TO_DATE(to_char(40825,'FM000000') ,'MMDDYY') FROM dual
Can someone explain to me why when I perform a LIKE select in SQL (T-SQL) on a varchar column I can do the following:
SELECT *
FROM Table
WHERE Name LIKE 'Th%'
to get names beginning with Th, but when I do the same on a datetime column I need a % before the year, like:
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
to get dates in 2013. The datetimes are stored in yyyy-MM-dd hh:mm:ss format. I know I could use a DATEPART style query but I was just interested in why I need the extra % here.
The DATETIME is converted to a VARCHAR before the comparison, and there definitely is no guarantee that the conversion will be in the pattern you mention. DATETIME is not stored internally as a VARCHAR but as a FLOAT.
You should stop wondering because the syntax is not useful.
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
Will give you a full table scan because the date will be converted to a varchar when comparing. In other words, don't do it !
Use this syntax instead:
SELECT *
FROM Table
WHERE Date >= '2013-01-01T00:00:00'
and Date < '2014-01-01T00:00:00'
If the Date field is in timestamp:-
SELECT *
FROM Table
WHERE year(Date) = '2013'
The sql server converts datetime to this format (Jan 1, 1900 9:20AM.)Because of that reason We need to use an extra %.
If you want to search the records start with month Jan
you can use following query for date time
SELECT *
FROM Table
WHERE Date LIKE 'Jan%'.
No need of extra '%'.
I have date field in the database in the format 2012-03-17 19:50:08.023.
I want to create a select query which gives me the data collected in the March month.
But I am not able to achieve this.
I am trying following query.
select * from OrderHeader where
Convert(varchar,UploadDt,103) between '01/03/2013' and '31/03/2013'
and DistUserUniqueID like '6361%'
This query gives me data for all the dates.
select * from OrderHeader where
UploadDt between '01/03/2013' and '31/03/2013' and DistUserUniqueID like '6361%'
This query gives me the error as Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Please help me resolve this.
Thanks in advance
The first query returns all dates because you are converting your column to a string. Not sure why you are doing this. So when you say BETWEEN '01/anything' AND '31/anything', when you consider it is now just a string, that is going to match all "dates" in the column, regardless of month and year, since your WHERE clause will cover every single day possible (well, with the exception of the 31st of months other than March, and the first day in January and February - so not all the data but a very large percentage). '15/11/2026', for example, is BETWEEN '01\03\2013' AND '31/03/2013'.
Think about that for a second. You have datetime data in your database, and you want to convert it to a string before you query it. Would you also convert salary to a string before comparing it? If so, the person making $70,000 will look like they are making more than the person making $690,000, since character-based sorting starts at the first character and doesn't consider length.
The second query fails because you are using a regional, ambiguous format for your dates. You may like dd/mm/yyyy but clearly your server is based on US English formatting where mm/dd/yyyy is expected.
The solution is to use a proper, unambiguous format, such as YYYYMMDD.
BETWEEN '20130301' AND '20130313'
However you shouldn't use BETWEEN - since this is a DATETIME column you should be using:
WHERE UploadDt >= '20130301'
AND UploadDt < '20130401'
(Otherwise you will miss any data from 2013-03-31 00:00:00.001 through 2013-03-31 23:59:59.997.)
If you really like BETWEEN then on 2008+ (you didn't tell us your version) you can use:
WHERE CONVERT(DATE, UploadDt) BETWEEN '20130301' AND '20130331'
More date-related tips here:
Dating Responsibly
Finally, when converting to VARCHAR (or any variable-length data types), always specify a length.
Rewrite the query as
select * from OrderHeader where
UploadDt between '01/03/2013' and '01/04/2013'
and DistUserUniqueID like '6361%'
or
select * from OrderHeader where
UploadDt between Convert(datetime,'01/03/2013', 103) and Convert(datetime,'01/04/2013',103)
and DistUserUniqueID like '6361%'
Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...