0

I am trying to select all the values from a table using date in SQL Server 2008. The problem is the date column in the DB is in Datetime format. But the thing is I have to select all the values from the database by using date

I used the following query

select * 
from Table 
where subdate = '2012-12-12'.

It won't return any value....

My table is like this

Subdate                        val1    val2    val3 name
--------------                -----   -----   ----  -----
2012-12-12 12:32:12.000     2   1   2    ben
2012-12-27 15:17:32.533     2   1   2    yen
2012-12-27 15:20:06.660     2   1   2    sun

Thanks in advance.........

4

3 に答える 3

2
select * from Tble where subdate>='20121212' and subdate <'20121213'

私の通常の推奨アプローチです(*)。パラメータの場合、次のようになります。

select * from Tble where subdate>=@Parm and subdate <DATEADD(day,1,@Parm)

'2012-12-12''2012/12/12'サーバーの言語/日付設定によっては、奇妙な変換の問題が発生する可能性があります。'20121212'常にとして変換されYYYYMMDDます。


(*)BETWEEN2つの真夜中を含めるか、翌日の真夜中前の最後の瞬間を計算する必要があるため、このような比較には使用しません。23:59:59.997を使用している場合はこれになりますdatetimeが、突然、のために別のものに変わりますdatetime2

于 2013-01-08T09:44:42.173 に答える
1
SELECT * 
FROM Table 
WHERE CAST(subdate AS DATE)='2012-12-12'

ラージ

于 2013-01-08T09:39:57.800 に答える
1

表の日付にも含まれTimeているため、その日付を入力する日付に適した形式に変換する必要があります。

以下を使用する必要があります: Sql Server Date Formats

select * from Tble where CONVERT(VARCHAR(10), subdate, 111) = '2012/12/12'

デモSQLFiddle

于 2013-01-08T09:41:29.230 に答える