select com datetime mssql

Asked

Viewed 46 times

1

I’m trying to select in ms sql but I can’t use datetime.

$dataInicio = \DateTime::createFromFormat('d/m/Y', $input['data_inicio'])->format('Y-m-d');

$result = \DB::select('Select * from PESAGEM where cast(databruto as date) = ? and ativo = 1')  ,[$dataInicio]); 

I tried to do by querybuilder and could not also, example if I use this direct query in the bank I get return.

select * from pesagem where cast(datatara as date) = '2015-09-03' and ativo = 1

How would make a query using format datatime?

  • What do you mean you can’t use datetime? try to explain your question better.

  • Well, I do not know what the problem but php does not return me anything when I do the query, the same query I do directly in the database but la passo a data entre aspas

  • Have you generated an error? If you are quoting in the database try this in php 'Select * from WEIGHING Where cast(raw date as date) = ''? '' and active = 1'

  • does not generate error, behind the empty variable, with quotes shows error in the result variable

  • What kind of column? datetime? smalldate?

  • It’s like datetime

Show 1 more comment

2 answers

1


SELECT 
   *
FROM
   Pesagem
WHERE 
   CONVERT(DATETIME, databruto, 103) = '01/10/2015' AND Ativo = 1

0

Manipulating data with mysql would be

select datatara as date from pesagem where DATE(datatara) = '2015-09-03' and ativo = 1

or

select datatara as date from pesagem where DATE_FORMAT(datatara, '%Y-%m-%d') = '2015-09-03' and ativo = 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.