European format time - Convert string to Datetime in SQL - sql

I want to be able to convert the following string format to Datetime.
'21-04-2010 11:06'
I've looked here but there doesn't seem to be solution: http://msdn.microsoft.com/en-us/library/ms187928.aspx
convert(datetime, '21-04-2010 11:06', ??)
Has anyone else come across this problem? Or is it listed somewhere else?

Try this:
select convert(datetime, '21-04-2010 11:06', 105)

select convert(datetime, '21-04-2010 11:06', 105)
Here the ref http://www.w3schools.com/sql/func_convert.asp Basically is dd-mm-yyyy format

You can use STR_TO_DATE date in MySQL to parse your String

If it is sql server, use 105...like this:
select convert(datetime, '21-04-2010 11:06', 105)
it will work (sql server docs says that it is for 21-04-2010 ...but it will get your 11:06, too)

Related

converting string format to date format

I am not able to convert the string to DATE format
select CONVERT(DATE, '8/17/2016', 103)
Conversion failed when converting date and/or time from character string.
what can we do to resolve this
thanks
Actually you don't need that style, It is the one causing the error. Just CAST/CONVERT it to date. Try this
select CONVERT(DATE, '8/17/2016')
You can try with cast also
select cast( '8/17/2016' as date)
SELECT CONVERT(DATE, '8/17/2016', 101)
SELECT convert(datetime, '8/17/2016', 111)

Date format dd/mm in SQL Server

I use SQL Server and I need to display a datetime data type in the following format:
dd/mm
day-month without the year, which is the most effective way?
Use 103 style in convert function and remove the year
SELECT LEFT(CONVERT(VARCHAR(15), Getdate(), 103), 5) --11/03
You'll find this site really helpful I think:
http://www.sql-server-helper.com/tips/date-formats.aspx
From that link, you can see this as a quick way to get DD/MM:
SELECT CONVERT(VARCHAR(5), GETDATE(), 3) AS [DD/MM]

Convert Text to Date in SQL Server

How to I convert a text value like "18/06/11" to a date format like "2011-06-18"?
I have tried the following
convert(char,[InstrumentText], 106)
but the value just stays in the same format
Thanks
The correct format for your string would appear to be 103.
More importantly, you need to convert to a datetime not to a char:
convert(date,[InstrumentText], 103)
If you then want to convert it back to a string in the format yyyy-mm-dd, you can do:
convert(varchar(10), convert(date,[InstrumentText], 103), 120)
Try
SELECT convert(datetime, '18/06/11' , 3)
I grabbed this from sqlusa.com:
SELECT CAST([InstrumentText] AS datetime)

sql datetime format

I am trying to convert a date into this format YYYY-MM-DD hh:mm:ss eg. 2007-01-05 23:00:00. But my SQL leaves the string unchanged, can somebody tell me what I am doing wrong?
select convert(varchar,'23/02/2008 00:00:00',120)
Try SELECT CONVERT(DATETIME, '23/02/2008 00:00:00'). All you're doing is converting a string into a string, which won't change anything.
your data type needs to be DATETIME, eg:
select convert(DATETIME, '23/02/2008 00:00:00', 120)
try this. Works for Transact-SQL.
select convert(datetime,'23/02/2008 00:00:00')
I hope it helps
maybe you're looking to do this:
CONVERT(varchar,'23/02/2008 00:00:00',100) 'yyyy-mm-dd hh:mm:ss'
select format(getdate(),'dd-MM-yyyy hh:mm tt')
Following is the syntax to achieve what you are looking for
SELECT CONVERT(VARCHAR(19), GETDATE(), 120)
To know more about the date formats see the link
http://www.sql-server-helper.com/tips/date-formats.aspx

How do I convert a datetime column to nvarchar with the format DD/MM/YYYY?

I need to select a datetime column in a table. However, I want the select statement to return the datetime as a nvarchar with the format DD/MM/YYYY.
Here is the convert documentation:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Looking through that, it looks like you want style 103:
SELECT CONVERT(nvarchar(10), getdate(), 103)
This should help. It contains all (or most anyway) the different date formats
http://wiki.lessthandot.com/index.php/Formatting_Dates
I think you'd be better off handling the string conversion in client if possible.
You can convert a date in many formats, in your case :
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 103) => 15/09/2016
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 3) => 15/09/16
Syntax:
CONVERT('TheDataTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
The code is an integer, here 3 is the third formatting option (without century), if you want the century just change the code to 103.
See more at: http://www.w3schools.com/sql/func_convert.asp
select CONVERT (NVARCHAR, GETDATE(), 103)
Look up convert in BOL.
Use Convert with the 103 option.
select convert(nvarchar(10), datefield, 103)