How to convert timestamp to date&time in SQL? - sql

Table have one column which contain timestamp value.I want to convert that timestamp value to date&time in SQL.
When I using like this
select cast(1520339311 as datetime)
I'm getting an error:
Arithmetic overflow error converting expression to data type datetime
How to handle that?

SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
See the links Below:
CAST and CONVERT (Transact-SQL)
rowversion (Transact-SQL)
Already Answered

Related

can we insert date into varchar2 in oracle without converting by to_char?

can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.

Why can't I insert yyyymmdd (stored as text) to a date column

This has been asked many times before and I tried quite a bit before posting this question. I have 2 columns on my SQL Server table where the dates are stored as CHAR(8) yyyymmdd format. When I try to insert this to another table with date column, I get:
Conversion failed when converting date and/or time from character string.
The typecasting that I used is:
,CAST(convert(char(10),qrda.BillFromDate,120) AS DATE) AS [BillStartDate]
,CAST(convert(char(10),qrda.BillToDate,120) AS DATE) AS [BillEndDate]
With the above code, I am trying to make it SQL Server readable date format and then typecasting it to DATE so that I can insert it to the destination table without any other transformations. Not sure where I am going wrong with this.
Since date are stored in the following format yyyyMMdd you can insert these values without any need to use CONVERT or CAST functions, since this format can be implicitly converted to DATE.
If the data contains invalid values such as 00000000, you can use TRY_CONVERT or TRY_PARSE function to convert these values to NULL
Example:
CREATE TABLE #TBLTEMP(datecolumn DATETIME)
INSERT INTO #TBLTEMP(datecolumn)
VALUES ('20180101')
INSERT INTO #TBLTEMP(datecolumn)
VALUES (TRY_PARSE('00000000' as DATETIME))
SELECT * FROM #TBLTEMP
Result:
From the example above, you can see that 20180101 was inserted succesfly without any casting, while TRY_PARSE function converted the invalid value 00000000 to NULL.
You can use the following syntax:
INSERT INTO TargetTable(DateColumn)
SELECT TRY_PARSE([CharColumn] as DATETIME
FROM SourceTable
References
Understanding SQL Server’s TRY_PARSE and TRY_CONVERT functions
TRY_PARSE (Transact-SQL)
Sorry for the trouble folks, but looks like this is bad production data where some values are literally '00000000'. I dont know how this flows into our system, but since the source table columns are CHAR(8), this is a valid value. Not so much for me.

Alter column from nvarchar to DateTime SQL Server

The data is present in the column in nvarchar format, I need to change the data type of that column to Datetime.
There is another problem the data saved has 2 formats one is dd/mm/yyyy and other is mm/dd/yyyy. How can I achieve this?
I have tried the following but obviously I have got an error.
Used
UPDATE SalesRegister
SET followup = PARSE(FollowUpDate AS datetime);
Error:
Error converting string value '20/07/2014' into data type datetime using culture ''
Can someone guide me with this query? I was thinking one more way of doing this which can be first converting the format in mm/dd/yyyy or dd/mm/yyyy and than apply the alter statement. But I don't know this either.

SQLite varchar comparison as date

I have two varchar field named StartDate(like 'MM/dd/yyyy') and StartTime(like 'hh:mm').
select * from Table "where StartDate<'MM/dd/yyyy'"
Can anybody help me with this query?
For your specific problem you will have to convert the varchar yourself and the answer can be found in this stackoverflow post: Sqlite convert string to date
sqlite would allow you to use a regular query for a date saved as a varchar - however, you will have to store the dates in the correct format - maybe you can change your date fields to follow this format? Have a look at the sqlite documentation: sqlite date functions.

inserting only date in a column in sql server

Is there a way to insert only date in a datetime column in sql server without the time?
for example
date (datetime)
===============
12-01-2000
16-02-2000
or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.
my solution is to store it as varchar and convert it to datetime whenever needed
SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only
or
also check this post about creating date type :
create user defined data types:
create type Date from dateTime
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
If you are using SQLServer 2008 you can use the date data type.
The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.
Select Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable
Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.
You can change format of date format in sql queries or in your app.
Here is a list on date formats in sql
http://www.sql-server-helper.com/tips/date-formats.aspx
Here's a link on date data types
http://databases.about.com/od/sqlserver/a/date_time.htm
Good Luck!