Convert varchar to datetime without millisecond - sql

Write simple this query:
select cast('2020-10-10 07:30:00.0000000' as datetime)
and want to show
'2020-10-10 07:30:00'
but SQL Server return this error:
Conversion failed when converting date and/or time from character
string.
How can solve that problem? Thanks.

A datetime doesn't have that level of precision, instead use datetime2:
select cast('2020-10-10 07:30:00.0000000' as datetime2);
And to only show the desired string, cast it to a shorter string:
select cast(cast('2020-10-10 07:30:00.0000000' as datetime2) as varchar(19));
Obviously it makes no sense to cast a string to date and back again, but I assume you have simplified your actual use-case. Otherwise you could use:
select cast('2020-10-10 07:30:00.0000000' as varchar(19));

Related

Convert date and/or time from varchar

How can I convert varchar string to date?
select CONVERT(date,'23/12/2019')
avoiding the following error:
Conversion failed when converting date and/or time from character string
Thanks
If you are using SQL Server (as suggested by the use of CONVERT() for this purpose), you can use CONVERT() with a third argument to specify DD/MM/YYYY format. That argument is 'DD/MM/YYYY'. No. Wait. That would be too sensible. You want:
select CONVERT(date, '23/12/2019', 103)
You are doing right except one thing which is missing from your statement and that is date_style similant.
select CONVERT(date, '23/12/2019', [put the similant here])
Example
select CONVERT(date, '23/12/2019', 110)
Result
2019-12-23
for full understanding
Click Here

Convert datetime string in datetime format in SQL server

I'm using MS SQL server and I have a date field of type text. The dates stored there are in this format
2017-03-01T18:23:02+0700
I'm trying to convert this field in a datetime field but I fail. I have tried
CONVERT(datetimeoffset,date, 127)
CONVERT(datetime,date, 127)
CONVERT(datetime2,date, 127)
but I keep getting
Conversion failed when converting date and/or time from character
string.
I think the problem is that according to ISO8601 the time offset must be in the format hh:mm while mine is hhmm. I don't mind keeping only the date (yyyy-mm-dd) if it is more easy.
I have read similar question but none matches exactly my case and I can't figure out the solution.
Try this
Declare #dt varchar(50)
set #dt = '2017-03-01T18:23:02+0700'
select convert(datetime, replace(LEFT(#dt, LEN(#dt) - 1), '+', '.'), 126)
If you need only date part then you can use below query
SELECT CAST(LEFT('2017-03-01T18:23:02+0700',10) as DATE)
Use Below query to convert datetime :
SELECT CONVERT(DATETIME,REPLACE(REPLACE('2017-03-01T18:23:02+070','T','
'),'+','.'),103)
For DATE only use below query :
SELECT CONVERT(DATE,REPLACE(REPLACE('2017-03-01T18:23:02+010','T','
'),'+','.'),102)
It doesn't seem to work in both ways (both raise error):
SELECT CONVERT(datetime,'2017-03-01T18:23:02+0700',127)
SELECT CONVERT(datetime,'2017-03-01T18:23:02+07:00',127)
It seems that it works only specifying Z as time zone:
SELECT CONVERT(datetime,'2017-03-01T18:23:02Z',127)

SQL Server Convert Varchar to Datetime #212342

I'm pretty sure there is an easy way of doing this, but I just cant figure it out. I'm trying to add a value which is stored as varchar to the current date.
In Oracle I'm using:
select employee_no
from activities
where EXPECTED_START > sysdate - (select value from params where name='before')
For SQL Server I get an error
Conversion failed when converting date and/or time from character string
Is there an easy way to do this as I've tried convert/dateadd but it doesn't seem to be able to get it to work.
select employee_no
from activities
where EXPECTED_START > getdate() - (select value from params where name='before')
By default SQL Server will automatically convert the varchar value to datetime if required.
You will get errors if your varchar columns have invalid character, fox example :
colVarchar
20140909
20150909
2013-05-05
wrong
...
???
As you see, the top 3 rows will be valid in the automatic conversion.
So you need to make sure you column, which store datetime as varchar, does not contain any invalid value.
Read more about conversion here
You can recreate your problem by simply using:
SELECT CURRENT_TIMESTAMP + '30'
This is because SQL Server is trying to convert '30' to a date, as defined by data type precedence.
Since in an expression all components must be of the same datatype, and DATETIME has a higher precedence that VARCHAR, before anything can be done the VARCHAR must be implicitly converted to a datetime, whereas what you actually want it converted to an int. So you need to do an explicit conversion:
SELECT employee_no
FROM activities
WHERE EXPECTED_START > GETDATE() - (SELECT CONVERT(INT, value) FROM params WHERE name='before');
Alternatively you could use the DATEADD function, which will force conversion to an integer:
SELECT employee_no
FROM activities
WHERE EXPECTED_START > DATEADD(DAY, (SELECT CONVERT(INT, value) FROM params WHERE name='before'), GETDATE());

varchar vs datetime in sql

I was having problem in retrieving from SQL Server so I posted this [question][1]
I did not get any suitable answers. So I have changed the column datatype from datetime to varchar and now it works fine.
SELECT *
FROM test
WHERE (timeStamp BETWEEN '05-09-2013 18:23:57' AND '05-09-2013 18:23:59')
But my query if varchar datatype can play the role of datetime and in varchar we can also store the string then why sql provides datetime datatype? I know varchar occupies more space than datetime. I would like to know other reasons.
Change datatype of your column to datetime. You can do your query IF you'll use datetime instead of varchar in where clause:
select *
from test
where timeStamp between convert(datetime, '2013-09-05 18:23:57', 120) and convert(datetime, '2013-09-05 18:23:59', 120)
I'm pretty sure it would work even with implicit cast if you use ISO format of date:
select *
from test
where timeStamp between '2013-09-05 18:23:57' and '2013-09-05 18:23:59'
Here's more info about cast and convert.
Another reason apart from space is this:
Datetime has other functions like picking up the day, year, month,hours,minutes,seconds etc so that you don't have to write it for yourself. If you use varchar then it will be your responsibility to provide functions for future use. You should use split function to retrive the part of date you want.
Another is that a query on a varchar works slower when compared to Datetime when you use to conditions to compare month / day/ year
Always use proper DATETIME datatype to store date and time values. Refer this for more information
http://beyondrelational.com/modules/2/blogs/70/posts/10902/understanding-datetime-column-part-iv.aspx

datetime conversion issue in sql server

if i try to cast string date to datetime like
select cast('12/01/2010' as datetime) then
it works
but if i try to cast like
select cast('22/01/2010' as datetime) then it is giving error.
again if i try to cast string date to datetime like
select cast('2010/12/01' as datetime) then it works
but if i try to cast like
select cast('2010/25/01' as datetime) then it is giving error. my requirement is whatever way user input date that should be successfully converted to datetime. please tell me best solution
In your case the sql server assumes, that date format id mm/dd/yyyy - US format,
you want to use French format, so use convert
select CONVERT(DATETIME, '12/01/2010', 103)
instead you'll get an error because there in no such a month number - 22
The best solution when passing datetime as string - to use short(without timezone) ISO format:
yyyyMMdd HH:mm:ss.ffff
I recommend you to use fixed '20111231' format. You do not need to use cast, convert or similar command.