D
D
Dmitry Bay2017-01-16 18:27:51
MySQL
Dmitry Bay, 2017-01-16 18:27:51

STR_TO_DATE in MSSQL 2008?

I'm working on improvements here, and I stumbled with mssql incompatibility on one piece of code, the request is this:
Initially, the database has year and month columns (such a tricky move from the previous developer), you can't edit these columns, but you need to make a selection between. How can it be made painless?
Two options are spinning in my mind:
1) how to modify STR_TO_DATE under MSSQL 2008
2) add a normal date field, and use it when selecting.

SELECT `tasks`.* FROM `tasks` RIGHT JOIN `tasks_kpi` ON tasks.id = tasks_kpi.tasks_id WHERE (((`tasks_kpi`.`kpi_id`=1) AND (`tasks`.`type`='1')) AND (tasks.status<>10)) AND (STR_TO_DATE( periodYear+'-'+periodMonth+'-01'),'%Y-%m-%d') BETWEEN '2016-01-01' and '2016-04-01')'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2017-01-16
@kawabanga

MSSQL has CAST and CONVERT
CONVERT - a more stringent
query tail could look like this:
AND (cast(( periodYear+'-'+periodMonth+'-01'),'%Y-%m-%d') as datetime2) BETWEEN '2016 -01-01' and '2016-04-01')
i.e. CAST( some bullshit AS some type) [ CAST('01-04-2017' as datetime2)]
or CONVERT(datetime2, '31-12-2017' , 104) (104 is the format DD-MM-YYYY)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question