CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 2896|Reply: 8

SQL - BETWEEN 2 DATES

[Copy link]
Post time 26-6-2013 05:11 PM | Show all posts |Read mode
Hi all,
nak tanya kenapa bila I run coding ne dlm sql, output dia x betul

SELECT     TARIKH_STR
FROM        EMPLOYEE
WHERE     (TARIKH_STR BETWEEN '01/01/2009' AND '31/01/2009')

output yg keluar caca marba  ada yg tahun 2008,1999..etc
btw, field TARKH_STR ne type  dia char(10).

tq .


Last edited by isms on 27-6-2013 09:17 AM

Reply

Use magic Report


ADVERTISEMENT


Post time 26-6-2013 10:15 PM | Show all posts
patek bukanlah arif sangat bab2 begini.. ramai lagi yang handal2 di luar sana.

cumanya char(10) itu ibarat seorang budak kecil yang dilepaskan kat dalam hutan belantara..

memang tidak akan jumpa jalan pulang ke rumah beliau.

philosophynya mudah saje.. char(10) dan date/datetime adalah dua entiti yang berlainan untuk mencari "tarikh di antara"

convert dahulu "TARIKH_STR" kepada date/datime kemudian barulah letakkan condition1 dan condition 2

Reply

Use magic Report

 Author| Post time 27-6-2013 09:24 AM | Show all posts
orang_letrik posted on 26-6-2013 10:15 PM
patek bukanlah arif sangat bab2 begini.. ramai lagi yang handal2 di luar sana.

cumanya char(10) i ...

Tapi, saya pun try run field yg type dia datetime menggunakan SQL dia atas,
tp keluar mesej error "The conversion of char data type to a datetime data type resulted in an out-of-range datetime value"

Reply

Use magic Report

Post time 27-6-2013 12:34 PM | Show all posts
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Reply

Use magic Report

Post time 27-6-2013 01:29 PM | Show all posts
Select * from  tableName  where convert(datetime,columname,103) between convert(datetime, Format(dtStartDate.Value, ,103) and convert(datetime, Format(dtendDate.Value,103)
Reply

Use magic Report

Post time 27-6-2013 10:43 PM | Show all posts
isms posted on 27-6-2013 09:24 AM
Tapi, saya pun try run field yg type dia datetime menggunakan SQL dia atas,
tp keluar mesej error ...

err, kalo dah field type datetime, untuk date between tu sila masukkan time sekali. contoh date 01/01/2009, taip '01/01/2009 00:00:00:000' .
Query dulu tengok macam mana data untuk field datetime tuh.

contoh -

select * from emp
where doc_date between '2012-01-01 00:00:00:000' and '2012-01-31 00:00:00:000'

p/s- x tau btl ke tak, try n error ja la.
Reply

Use magic Report

Follow Us
Post time 28-6-2013 11:44 AM | Show all posts
Ye, betul tu, field yang ada date patut guna type date bukan char.
Lagi satu kalau nak query data date, kene ade between.

Harap membantu.
Reply

Use magic Report

Post time 31-7-2013 12:57 AM | Show all posts
bila main ngan 'date' dlm SQL, sumtimes mmg memeningkan.

mcm reply kt atas, kalo field utk simpan date, sebaiknya, field type pakai 'date/time'


biasa kita guna 'function date' utk cri beza antara dua date..


apa itu function date ?

secara ringkas, ia adalah built-in function dalam SQL utk proses date yg kita simpan dlm database.

cth function date yg ada... DateDiff, DateFormat.. bla.. bla.. bla

cth : http://www.w3schools.com/sql/sql_dates.asp



tambahan, jgn salah paham bahawa hanya utk date je ada built-in function..

utk benda lain yg kita simpan dlm d/base pun ada built-in function jugak.

tgk pd kesesuaian & kehendak output akhir, function apa yg kita nk guna.

Reply

Use magic Report


ADVERTISEMENT


Post time 18-9-2013 03:49 PM | Show all posts
kalau nak mudah. ni contoh dalam oracle


select *
from table a
where to_char(tarikh_mengawan, 'yyyymmdd') between '20130101' and '20130131'
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT


Forum Hot Topic

 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

29-12-2024 12:11 PM GMT+8 , Processed in 0.444201 second(s), 21 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list