0

I know there are many questions of this kind, and I have already seen another questions as this one: Conversion failed when converting date and/or time from character string while inserting datetime

But the main thing is that I cannot convert a specific string to datetime using any format using SQL Server. Here is what I'm trying to do:

DECLARE @1 AS VARCHAR(500)
SET @1 = '18/03/2015 08:07:25 a.m.'

SELECT CONVERT(DATETIME, @1, 131)

This is not working, even if i tried with several formats as specified here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

For what I've tried it just works with 131 format if the string containing the datetime is on 24 hour format, like this:

DECLARE @1 AS VARCHAR(500)
SET @1 = '18/03/2015 08:07:25'

SELECT CONVERT(DATETIME, @1, 131)

Does anyone know how can I do to convert that string to datetime as I need?.

Many thanks for your help.

Community
  • 1
  • 1
NicoRiff
  • 4,803
  • 3
  • 25
  • 54

1 Answers1

1

You are not specifying the @1 to be in the correct format, that's all

DECLARE @1 AS VARCHAR(500)
SET @1 = '18/03/2015 08:07:25 am'
SELECT CONVERT(datetime, @1,103)

Please see this webpage for more on formatting

https://msdn.microsoft.com/en-us/library/ms187928.aspx

JustAPup
  • 1,720
  • 12
  • 19