㈠ SQL中,如何查詢年齡
日期函數,access與SQL Server是有一點區別的,
如果是access的話,
select * from 你的表名
where year(now())-year(出生日期) between 20 and 25
如果是SQL Server的話,
select * from 你的表名
where year(getdate())-year(出生日期) between 20 and 25
呵呵,希望能有幫助,^_^
㈡ sql 急求工齡計算語句
寫了一個函數,可以幫助大家 計算工齡。調用方式 select fn_GetWorkYear('2015-10-10','2019-03-20')。
create function [dbo].[fn_GetWorkYear]
(
@beginday datetime, --開始日期
@endday datetime --結束日期
)
returns int
as
begin
if @beginday > @endday
begin
return 0;
end
declare @workyear int
select @workyear = datediff(year, @beginday, @endday)-1--年份差值
if datepart(month, @endday) > datepart(month, @beginday)--月份超過
begin
select @workyear = @workyear + 1
end
if datepart(month, @endday) = datepart(month, @beginday)--月份一樣
begin
if datepart(day, @endday) >= datepart(day, @beginday)--日超過
begin
select @workyear = @workyear + 1
end
end
return @workyear ;
End
GO
㈢ 查詢工作超過5年的員工信息的sql語句
SELECT 員工.姓名 form 員工 WHERE 員工.工作年限>5;
㈣ sql中如何提取從資料庫中所獲得時間的年份
SQL從時間欄位值中獲取年份使用DATENAME()函數。
DATENAME()函數語法:DATENAME(param,date)。
date是時間欄位名 或一個時間值param是指定要返回日期部分的參數,包括下面幾種:
獲取年份就可以這樣寫 datename(year,date) 或 datename(yy,date) 。
已系統當前時間getdate()為例,3種寫法獲取年份。另外,DATENAME返回的是一個字元串,如果需要返回整數,可以使用DATEPART ( datepart , date ) ,語法與DATENAME相同。
獲取日期欄位的年select to_char(sysdate,'yyyy') as year from al或者:(指定日期)select to_char(to_date('2013/08/08','yyyy/mm/dd'),'yyyy') as year from al
獲取日期欄位的月select to_char(sysdate,'mm') as month from al
獲取日期欄位的日select to_char(sysdate,'dd') as day from a
㈤ sql查詢數據,好多都需要按年份查詢,按年份查數據有哪幾種方式呢
本人時間戳都不知道是什麼東西i,網路看了一下,還是不懂,慚愧呀!
你可以打開sql 2005在聯機幫助中搜一下,有很多函數。
我知道其中有一個很有用,DATEDIFF ( datepart , startdate , enddate ):
Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate.就是用於在startdate和enddate年份之間數據不是連續存在的情況,舉個聯機幫助上的例子吧,希望能幫到你,我是學生可能沒有經驗:
CREATE TABLE dbo.Duration
(
startDate datetime2
,endDate datetime2
)
INSERT INTO dbo.Duration(startDate,endDate)
VALUES('2007-05-06 12:10:09','2007-05-07 12:10:09')
SELECT DATEDIFF(day,startDate,endDate) AS 'Duration'
FROM dbo.Duration;
-- Returns: 1
㈥ 怎麼在sql中 查詢1年的數據
近一年分三種情況,以當前時間為中心,前後各半年;以當前時間為最後時間,查詢當前時間前一年的數據;以當前時間為起始時間,查詢後一年的數據。
語法分別如下:
1、以當前時間為中心,前後各半年
1
select * from 表名 where 時間欄位 between dateadd(DAY,(-364/2),GETDATE()) and dateadd(DAY,(364/2),GETDATE());
2、以當前時間為最後時間,查詢當前時間前一年的數據
1
select * from 表名 where 時間欄位 between dateadd(DAY,-365,GETDATE()) and GETDATE() ;
3、以當前時間為起始時間,查詢後一年的數據
1
select * from 表名 where 時間欄位 between GETDATE() and dateadd(DAY,365,GETDATE());
㈦ sql怎麼按年月查詢
按年:
select * from table where substring(convert(varchar(30),時間欄位,120),1,4)='2013'
按月:
select * from table where substring(convert(varchar(30),時間欄位,120),1,7)='2013-03'
歡迎追問
㈧ 查詢每個部門中的員工數量、平均工資和平均工作年限,sql語句,Oracle資料庫。
--平均服務來期限(單位為年)源
select
deptno,trunc(avg((sysdate-hiredate)/365),0)
"平均工作年限"
from
emp
group
by
deptno;
--不滿一年算一年
select
deptno,trunc(avg(trunc((sysdate-hiredate)/365,0)),0)
"平均工作年限"
from
emp
group
by
deptno;
--不滿一年不算
㈨ sql查詢工齡大於或等於10年的員工信息
select *
from 表
where datediff(yy,開始工作日期,getdate()) >= 10
㈩ SQL查詢日期的年份
要返回正確的記錄,你需要適用日期和時間范圍。有不止一種途徑可以做到這一點。例如,下面的這個SELECT 語句將能返回正確的記錄: SELECT * FROM weblog WHERE entrydate>=」12/25/2000」 AND entrydate<」12/26/2000」 這個語句可以完成任務,因為它選取的是表中的日期和時間大於等於12/25/2000 12:00:00:000AM並小於12/26/2000 12:00:00:000AM的記錄。換句話說,它將正確地返回2000年聖誕節這一天輸入的每一條記錄。 另一種方法是,你可以使用LIKE來返回正確的記錄。通過在日期表達式中包含通配符「%」,你可以匹配一個特定日期的所有時間。這里有一個例子: SELECT * FROM weblog WHERE entrydate LIKE 『Dec 25 2000%』 這個語句可以匹配正確的記錄。因為通配符「%」代表了任何時間。