Convert time in SQL Server with the format: 07/19/2017 11:00:00 a. m

0

I have the following script to insert in the base:

INSERT INTO k_horario(OIDHorario, OIDEmpleado, OIDSalon, OIDTaller, Hora, Dia, Semana, Duracion, FechaCreacion) VALUES ('3de7d494-5de9-4ab5-8d30-816103f1a9a0', '396983d6-63bd-4a0d-82fa-fb2fdac90a6e', 'a574f65e-0e81-46ed-9a91-3d40302a6319', '6958a0d9-90e8-4e09-9196-a3523edc9589', '17/07/2017 06:00:00 a. m.', 1, 29, 60, '14/07/2017 09:42:31 a. m.')

The problem is that when I execute it, it throws the following error:

Msg 241, Level 16, State 1, Line 1 Conversion failed when converting datetime from character string.

I would greatly appreciate your help.

    
asked by Guillermo Ricardo Spindola Bri 25.07.2017 в 02:15
source

2 answers

1

If you execute this

select ISDATE('17/07/2017 06:00:00 a. m.')

You will realize that it is not a valid date format, so use an ISO format like the following:

select ISDATE('20170717 06:00:00')
select ISDATE('20170717 16:00:00')
    
answered by 25.07.2017 в 21:41
0

From what I see in your table you have a problem in what is the time and date

For the time

select convert(datetime, RTRIM(LTRIM(SUBSTRING('17/07/2017 06:00:00 am', 11, 20))))

For the date

select convert(datetime, RTRIM(LTRIM(SUBSTRING('17/07/2017 06:00:00 am', 1, 10))))
    
answered by 25.07.2017 в 02:46