how to skip sql timestamp to get records for specific date - sql

I want to compare the date in my sql query and get the records for a specific date, but it is stored in timestamp format in postgres.
What kind of query could compare only date parts, since I want and skip the time part.

This Query will give you date like '2012/11/22' which you can use for date comparison.
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
//OR simply
select CAST(GETDATE() as DATE)
You can see more formats here :- CAST and CONVERT (Transact-SQL)

Try this:
SELECT *
FROM tab
where date_col::DATE BETWEEN '2012-01-01'::DATE AND '2012-01-05'::DATE
SQL Fiddle DEMO

Related

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

Is it possible to get the timestamp of a SQL datetime column?

I am using Microsoft SQL Server and I have a table with a datetime column.
When I run a query SELECT END_DATE FROM CUSTOMER I get 2007-12-03 10:15:30.000
I would like to run a query that returns a unix timestamp like 1543357818 representing the date and time. Is this possible through SQL?
Just use datediff():
SELECT DATEDIFF(second, '1970-01-01', END_DATE)
FROM CUSTOMER
As an alternative, convert to float and do a subtract, then convert to seconds. Obviously you can change some of this to constants, but this illustrates what I'm doing:
select floor((convert(float,getdate())-convert(float,convert(datetime,'1970-01-01')))*24*60*60)

Convert datetime to just date in SQL

I want to select a datetime stamp column from a table, but select it just in date format. I tried SELECT TO_CHAR(Column,'MM/DD/YY') but it did not work.
Any suggestions?
Use trunc():
select trunc(column)
This removes the time component in Oracle.

SQL query doesnt bring out results when queried using date

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')

SQL datetime LIKE select - why do I need an extra %?

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 '%'.