sql datetime formats conversion - sql

How can i convert a datetime of type dd-mm-yyyy to dd/mm/yyyy in sql?
I dont want to convert to varchar but to datetime.
Can anyone give me an idea please?

A DateTime is a DateTime is a DateTime - it doesn't have " a format" - it's just a DateTime.
The format or display options only come into play when you want to display the DateTime as a string.
of type dd-mm-yyyy to dd/mm/yyyy
To me, this sounds as if:
you have string representing a date, in the format dd-mm-yyyy
you want to convert that to a DateTime and then again display a string representing that DateTime in the dd/mm/yyyy format
Right??
Check out the MSDN SQL Server Books Online for CAST and CONVERT - it lists all possible, valid, supported string formats representing a date and time.
Format dd-mm-yyyy is style no. 105, so this will convert your first string:
SET #YourDateTime = CONVERT(DATETIME, '23-11-2010', 105)
Now you have your DATETIME value (Nov 23, 2010) in #YourDateTime - just a DateTime, no formatting applied.
If you want to display that date value in format dd/mm/yyyy, which is style no. 103, use this snippet of T-SQL to achieve this:
SELECT CONVERT(VARCHAR(50), #YourDateTime, 103)
This will display 23/11/2010.

Related

How to convert varchar to datetime format in SQL Server

I am trying to convert varchar date value to datetime using this query
SELECT
CONVERT(varchar, CONVERT(datetime, [Assigned_DT]), 105) + ' ' +
CONVERT(varchar(8), CONVERT(datetime, [Assigned_DT]), 14)
But for this date '16/02/2021 1:55 PM' I get this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
How can I convert so it is valid - the format of the dates are mm/dd/yyyy hh:mm:ss
The 105 number in the code is the wrong style number. It's for Italian dd-mm-yyyy values (note the - vs /). You need to read the CONVERT() function docs and find the style number that matches your input. I could do that for you, but now that I've pointed you where to look, doing it yourself is good practice.

How to convert date time format in SQL Server like '2017-03-04 10:07:03.490' to date format which is seperated by - like '2-11-2016'

I am tying to make date comparison with the query
select *
from dbo.OH_Case
where dbo.OH_Case.CreatedDate between Convert(varchar(30),'24/04/2017', 102)
and Convert(varchar(30),'01/05/2017', 102)
but it throws this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
My application passes data in '01/05/2017' format and the date information in SQL Server is stored in '2017-03-04 10:07:03.490' format. What should I do to make both of these in same format which allows comparison?
First, you're converting a string literal to a varchar. Convert it to a datetime instead.
Second, you're passing in 24/04/2017, but date format 102 is yyyy.mm.dd. Try date format 103 for dd/mm/yyyy:
convert(datetime, '24/04/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter.
convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between cast('24/04/2017' as datetime) and
cast('01/05/2017' as datetime)
Updates:
Siddharth, I have checked you date values and figured out that issue is in date "24/04/2017". This format is for "MM/dd/yyyy". and 24 is passed as month which is not valid. Therefore it is throwing exception. I have updated result
to handle such exceptions.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between convert(datetime, '24/04/2017', 103) and
convert(datetime, '01/05/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter. convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from dbo.OH_Case format(where dbo.OH_Case.CreatedDate, 'dd/mm/yyyy') as CreatedDate

convert varchar to datetime field

I'm trying to filter some records by date but the datetime field seems to be stored as varchar and I'm struggling to convert it. Below is a sample of the data:
ID DateField
0002 14/04/1989 01:30
0003 16/04/1989 09:45
0004 16/04/1989 06:00
0005 19/04/1989 01:07
0006 21/04/1989 16:03
When I use
cast(Datefield as datetime)
I get the following error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Edit - this does actually display results in datetime format but with the error message, why is that?
What's the best way to convert my datefield to valid datetime format? Thanks
Use CONVERT with Style
CONVERT(DATETIME,Datefield ,103)
To find bad data, see what this returns
set dateformat dmy
select Datefield from table where isdate(where)=0
You should always use proper DATETIME datatype to store datetime values
Try this. You need to add style part to identify the format
SELECT CONVERT(DATETIME, dates, 103)
FROM (VALUES ('14/04/1989 01:30'),
('16/04/1989 09:45'),
('16/04/1989 06:00'),
('19/04/1989 01:07'),
('21/04/1989 16:03')) cs (dates)
see CAST and CONVERT
you need to use
select CONVERT(datetime,your_Datefield ,103)
because your your_Datefield holding the datetime value in dd/mm/yy format.
follow the link, you'll get the explanation.
You can get the error because of by default datetime format is mm/dd/yyyy. So If your value have this format, then it easily convert without error.
For this you have to convert with format which convert the string into proper date-time format. For this cast will not work , but convert work. Here is link for more details.
As your format is dd/mm/yyyy, you need to use 103 format. Just check this to understand.
Select convert( datetime, getdate(), 103)

SSRS - Conversion of a varchar data type to a datetime data type resulted in an out-of-range value. - When converting to YYYY-MM-DD

I am running a query in SSRS which defaults the date picker date format to DD/MM/YYYY. However the dates in the actual database are in YYYY-MM-DD format. I have tried to convert these dates as below but I am getting the message "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value".
Col1 BETWEEN convert(varchar, '22/10/2013 00:00:00', 120) AND convert(varchar, '22/10/2013 23:59:59', 120)
I'm having some trouble working out why that error is triggering. Ie. 22/10/2013 is a valid date format so I was thinking it should convert into 2013-10-2013 easily enough?
But I guess I'm missing something!
DateTime values do not have a format - you apply a format when you display them. You should just be able to set the parameter to a DateTime type and just pass it into the query without any string conversion.

Sql query for CONVERT datetime to a different datetime Format in SQL 2005?

My datetime column is in MM/dd/yyy format. i want this to be converted in dd/MM/yyyy, I am able to converting this datetime to narchar.
select convert(nvarchar,columnname,103) from dbo.table
i m getting my answer like "dd/MM/yyyy" but it is string. if i write datetime in place of nvarchar
i am getting "yyyy-MM-dd" whaa is the query for getting "dd/MM/yyyy"
http://msdn.microsoft.com/en-us/library/ms187928.aspx
check out
see Microsoft's explanation on date formats
the one you're looking for is 20 or 120
BTW, you should supply with varchar with a number like this varchar(10)