Sunday, February 19, 2012

about select between help pls

well i have 2 input date form and to.

"date_from" varchar variable

and "date_to" varchar variable

where in i want to select only the dates between the two variables only.

example

date_from -date_to contains= "200702"-"200705"

and everything that starts from 200702 to 200705 will be output.

can someone help me or give some similar examples regarding this?

thanks...

Select * from Transaction Where Date_From >= (Cast(Year(TransactionDate) as varchar) + Cast(Month(TransactionDate) as varchar)) AND Date_To <= (Cast(Year(TransactionDate) as varchar) + Cast(Month(TransactionDate) as varchar))|||

My suggestion is not to use the BETWEEN operator, but instead use the >= and < operators. This, along with adding 1 day to the upper bounds of the "to" date, will help to avoid the pitfalls you can encounter when trying to work around the time portion of dates.

Try something like this:

DECLARE @.date_from datetime

DECLARE @.date_to datetime

SELECT @.date_from = '20070201', @.date_to = '20070530' -- note the the time portion of the date will default to midnight 00:00:00.0000

SELECT @.date_to = DATEADD(d, 1, @.date_to) -- add 1 day to the ending date

SELECT

someColumns

FROM

someTable

WHERE

myDate >= @.date_from AND myDate < @.date_to

|||

how about this one?

Public Sub read_records(ByVal nyutancd As String, ByVal date_from_new As String, ByVal date_to_new As String)

'配送依頼のデータ取得
'SQL作成
db = New TDataCenter.db.TDataCenter("venus", "venus", "threesupport", "postgres", "postgres")

date_from = ""
date_to = ""
syain_name = ""
product = ""

Dim objReader As PgSqlDataReader

'objReader = db.GetSQLReader(String.Format("Select distinct urr_urinenget,urr_etancd,urr_scd,urr_urikbn,urr_zurgsuu,urr_zurgkin,urr_zsirgkin,urr_curgsuu,urr_curgkin,urr_csirgkin,urr_gurgsuu,urr_gurgkinn,urr_gsirgkin,urr_turgkin,urr_tsirgkin,urr_kousindate from urr")))
objReader = db.GetSQLReader(String.Format("Select distincturr_urinenget,sn_syainnm,sy_snm,urr_urikbn,kt.kt_nm fromurr inner join sy on sy.scd = urr.urr_scd inner join sn on sn.syaincd = urr_etancd inner join kt on kt.kt_cd = urr.urr_urikbn and kt.kt_kbn='cm07'where urr_urinenget between {0} and {1} group by urr_urinenget,sn_syainnm,sy_snm,urr_urikbn, kt.kt_nm ", TDataCenter.db.TDataCenter.SingleQuatedStr(date_from_new), TDataCenter.db.TDataCenter.SingleQuatedStr(date_to_new)))

do you think it will work?

|||

natasha_arriell:

do you think it will work?

That approach is insecure. Do not use placeholders and string substitution; always use parameters.

I have no idea what processing SingleQuatedStr does to those string values. I have no idea what those string values contain. And I have no idea what sort of data is stored in the urr_urineget column. Your best bet is to use some edge case data and try it yourself to see if you are getting the desired result.

No comments:

Post a Comment