Convert datetime to just date in SQL - 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.

Related

Convert decimal to date format SQL

I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS Sql Server.

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)

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

Convert mm/dd/yy hr:min:sec to yyyy-mm-dd hr:min:sec

I want to convert (this is as in Syabase SQL)
'09/29/2012 08:23:00' to '2012-09-29 08:23:00'
This what i have tried but failed
select convert(varchar,convert(datetime, '09/29/2012 08:23:00', 101),123)
Try 120 instead of 123.
select convert(varchar(20),convert(datetime, '09/29/2012 08:23:00', 101),120)
See http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc38151.1510/html/iqrefbb/Convert.htm
Im not sure where you are getting that date format from but if it is from a table in the database what you can do is use the following to convert the date directly from the table.
Lets assume the date is in a table called MyTable and the date column is called MyDate
SELECT dateformat(MyDate, 'YYYY-MM-DD HH:MM:SS')
FROM MyTable;
You can test this out with the current datetime using the following:
SELECT dateformat(getdate(), 'YYYY-MM-DD HH:MM:SS');

how to skip sql timestamp to get records for specific date

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