AndreaNobili AndreaNobili - 1 year ago 58
SQL Question

How can I select a row having a BigInt and a DateTime field starting with a specigic value?

I am working on a Microsoft SQL Server database and I have the following problem to implement these 2 simple select query.

So I have a table named

that has a column
of type
with a value of

So for example I want search all the record in this table that have the
starting with the value

I tried to use
in this way:

select *
from MyTable
where OtherFieldFK like = '70%'

but it doesn't work. I think that like clause works only on string, is it right?

In this table I also have a
column named

I tried to do the same thing:

select *
from DataInizioGestione
where OtherFieldFK like = '2016%'

but in this case it doesn't work either.

How can I correctly implement these 2 queries?

Answer Source

the first should be right, as you wrote:

select * from MyTable where OtherFieldFK like = '70%'

for the second should be converted to the date format in the nvarchar (es 121 with this format aaaa-mm-gg hh:mi:ss.mmm(24h)); in this way you can make the comparison with the like:

select * from MyTable where convert(nvarchar,DataInizioGestione,121) like = '2016%'

or you can directly compare the year:

select * from MyTable where year(DataInizioGestione) = 2016
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download