I get 0 results if I query the timestamp column for entries that contain 2017 (or any other year).
The data in w_presse_erscheinung is a "timestamp without timezone"
and looks like this for example: 2017-06-01 00:00:00
I assume there is something wrong with the LIKE part of the query ...
My query looks like this:
$result = pg_query("
SELECT
name,
w_presse_link,
w_presse_erscheinung,
w_presse_quelle,
description
FROM adempiere.w_presse
WHERE isactive ='Y'
AND description='PS' AND w_presse_erscheinung LIKE '%2017%'
ORDER BY w_presse_erscheinung
DESC
")
Instead of relying on string conversions, you could explicitly extract the year from the timestamp:
EXTRACT(YEAR FROM w_presse_erscheinung) = 2017
You cannot use like with a timestamp field like that. Either typecast the timestamp field to varchar then use like or use extract function to match the year from the field.
Related
I have some tables that I cannot modify and a field "STAT_DATE" that is formatted as a short text like "yyyymmdd". I tried the Month(now()) functions and this did not work because the field is not in date format. I have also tried using the mid and monthname functions to try and match the current months name with the name of the digits that are meant to be the month in the string.
Use Like to compare your date text to the current date formatted as 'yyyymm\*'
Here is an Immediate window session to demonstrate the technique:
' give STAT_DATE today's date ...
STAT_DATE = "20220913"
' display today's date as the yyyymm* pattern ...
? Format(Date(), "yyyymm\*")
202209*
? STAT_DATE Like Format(Date(), "yyyymm\*")
True
Then use that approach in a query:
SELECT y.*
FROM [Your Table] AS y
WHERE y.STAT_DATE Like Format(Date(), 'yyyymm\*');
If you're using ADO/OleDb to run your query, use % instead of * as the wildcard:
WHERE y.STAT_DATE Like Format(Date(), 'yyyymm\%');
I see that a table has the data value as 18-May-2012. But when I query looking for the same date using the below query, no results are available.
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = '18-May-2012'
Could you help me sort this issue?
You need to convert string date into date with TO_DATE() function.
Also you need to take into account that your date might contain hours/minutes/seconds. In order to handle this you need to truncate submit_dt column.
In your case it would look like this:
Select Submit_Dt From Siebel.S_Order_Dtl
where TRUNC(submit_dt) = TO_DATE('18-May-2012','dd-MON-yyyy')
Try to convert the date to date format using to_date as below
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = to_date('18-May-2012','DD-MON-YYYY')
I am stuck in this query. The below part "TRANSMITTED_DATE LIKE '17-JUL-14'" is hardcoded. I want to make it a generalized one.
I want to do something like this
WHERE CAST(TRANSMITTED_DATE AS DATE FORMAT 'DD-MMM-YY') BETWEEN
CAST(CURRENT_DATE AS DATE FORMAT 'DD-MMM-YY')
AND CAST(CURRENT_DATE AS DATE FORMAT 'DD-MMM-YY')-7
i.e the last seven days record from the last time this query is run against the table.
But, the transmitted date is VARCHAR field in the table and format is like 31-OCT-13 i.e DD-MMM-YY.
Please help me with the query.
SELECT
MLI_MDL_NUMBER as DOCUMENTUM_MLI
,TRANSMITTAL_NUMBER
,PROJECT_ID
,TRANSMITTED_DATE
FROM GEEDW_PLP_BULK_V.CDR_DOCUMENTUM_TRSMTL
WHERE
( TRANSMITTED_DATE LIKE '17-JUL-14' OR
TRANSMITTED_DATE LIKE '18-JUL-14' OR
TRANSMITTED_DATE LIKE '19-JUL-14' OR
TRANSMITTED_DATE LIKE '20-JUL-14' OR
TRANSMITTED_DATE LIKE '21-JUL-14' OR
TRANSMITTED_DATE LIKE '22-JUL-14' OR
TRANSMITTED_DATE LIKE '23-JUL-14' OR
TRANSMITTED_DATE LIKE '24-JUL-14' OR
TRANSMITTED_DATE LIKE '25-JUL-14'
);
Depending on a global system setting (Century Break in dbscontrol) or your Teradata release this might be easy.
If you're on TD14 you can utilize Oracle's TO_DATE:
WHERE TO_DATE(TRANSMITTED_DATE, 'dd-mon-yy')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
Otherwise try following query and check what's returned:
SELECT CAST('17-JUL-14' AS DATE FORMAT 'DD-MMM-YY')
If it's 2014-07-17 you can simply use
WHERE CAST(TRANSMITTED_DATE AS DATE FORMAT 'DD-MMM-YY')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
If it's 1914-07-17
WHERE CAST(SUBSTRING(TRANSMITTED_DATE FROM 1 FOR 7) || '20' ||
SUBSTRING(TRANSMITTED_DATE FROM 8) AS DATE FORMAT 'DD-MMM-YY')
BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
Of course all go them are bad because every row needs to be converted from string to date, which is a big overhead and you'll loose an existing statistics.
If this is a big table and you need to run that query often you should try to change the datatype to a DATE and do the typecast once during import.
And there's another problem: if there's any string representing a non-valid date your query will fail.
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 a Date type column where are values in this format
1.1.2012 10:10:11
I need to create a filter which would filter these values by day, month and year.
I've tried
where like '% 1.1.2012 %'
but this seems to not working.
Oracle not store your date field formatted, but you can format the output with to_char function. For example:
select to_char(date_field,'dd/mm/yyyy hh24:mi:ss')
If you query a date without formatting, the output format will depend on the tool that you are using and your NLS_DATE parameter too.
To filter dates in Oracle you can use the to_date function, that receives an string and parse to date with some specific format. You can see all options of to_date here
Options to filter your date field:
where date_field between to_date('1.1.2012 00:00','d.m.yyyy hh24:mi') and to_date('1.1.2012 23:59','d.m.yyyy hh24:mi')
-- you possibly will lost some performance with this second one
where trunc(date_field) = to_date('1.1.2012','d.m.yyyy')
In MSSQL, you can use date-functions, that are easy to handle. One way would be like this:
where Year (date) = 2012
and Month(date) = 1
and Day (date) = 1
But there are other solutions. Take a look at the following page for mor information:
http://msdn.microsoft.com/en-us/library/ms186724.aspx
I worked recently with string-representations of datetime-values. I recommend not do it and to work always with the dates, because of compatibility, speaking of the MSSQL-Server.
If you use string-representations of datetime-values you need to be very careful with formats on different language-settings than the one on your own server.
Strings can be interpreted different on other servers (ISO-format vs us-format).
One possibility would be to do something like this:
WHERE date_and_time >=to_date( '01.01.2012','dd.mm.yyyy') and date_and_time <= to_date('01.01.2012','dd.mm.yyyy');
date_and_time is the name of your Date column.
edit: This is for Oracle