简体   繁体   English

如何使用转换函数sql(字符串到日期)

[英]how to use convert function sql (String to Date)

I have imported a calendar table from datameer over to my sql server database. 我已将日历表从datameer导入到我的sql server数据库中。 I am fairly new to SQL so I do not have the best understanding of the convert function. 我对SQL相当陌生,因此我对Convert函数没有最好的了解。 I have a calendar table with 5 columns (cal_month, cal_year, Toronto, Montreal, National_Avg). 我有一个包含5列的日历表(cal_month,cal_year,多伦多,蒙特利尔,National_Avg)。 The data types are (nvarchar, nvarchar, numeric, numeric, float). 数据类型为(nvarchar,nvarchar,数字,数字,浮点数)。

Example of a row in the table would be: 表中的一行示例为:

  Month  | Year | Toronto | Montreal | National Average
---------+------+---------+----------+------------------
 January | 2018 | 20      | 21       | 20.5

Below is my attempt at the convert function, my query is supposed to identify current month and -1 to grab the national average from the previous month so in this instance it would be for February 2018. 以下是我尝试使用convert函数的情况,我的查询应该确定当前月份,并以-1来获取上个月的全国平均水平,因此在这种情况下将是2018年2月。

select convert(date, cal_month), convert(date,cal_year), National_Avg 
from dbo.CALENDAR
where month(cal_month) = month(dateadd(month,-1,current_timestamp))
             and year(cal_year) = year(dateadd(month,-1,current_timestamp))

The error I am receiving is: 我收到的错误是:

Conversion failed when converting date and/or time from character string.

You don't need to manipulate the data in the table. 您无需操纵表中的数据。 You just need to manipulate your search parameter current_timestamp to fit the data's data-types. 您只需要操纵搜索参数current_timestamp以适合数据的数据类型。

SELECT cal_month, cal_year, National_Avg 
FROM dbo.CALENDAR
WHERE cal_month = DATENAME(mm,   DATEADD(month,-1,current_timestamp))
  AND cal_year  = DATENAME(yyyy, DATEADD(month,-1,current_timestamp))

The problem is probably that you're trying to convert a month/year integer to a date. 问题可能是您正在尝试将月份/年份的整数转换为日期。

Try: 尝试:

SELECT DATEADD(month, cal_month -1, DATEADD(year, cal_year-1900, 0)) 
from dbo.CALENDAR

Or if you need to get the month/year from today, try 或者,如果您需要从今天开始获取月份/年份,请尝试

DECLARE @oneMonthAgo datetime 
SET @oneMonthAgo = DATEADD(MONTH, -1, CURRENT_TIMESTAMP) 

DECLARE @cal_month int 
SET @cal_month = MONTH(@oneMonthAgo)

DECLARE @cal_year int 
SET @cal_year = YEAR(@oneMonthAgo)

SELECT National_Avg 
FROM dbo.CALENDAR
WHERE cal_year = @cal_year  AND cal_month  = @cal_month 

Ah, just noticed you use the monthname - that is stupid, but anyway: 啊,刚注意到您使用了月名-这很愚蠢,但无论如何:

DECLARE @cal_month nvarchar(200)
SET @cal_month = DATENAME(month, @oneMonthAgo)

Everything else stays the same. 其他一切保持不变。

Or you could add a computed column to the calendar table: 或者,您可以将计算列添加到日历表中:

ALTER TABLE dbo.Calendar ADD MonthNum AS  

    CASE Month 
        WHEN 'January' THEN 1 
        WHEN 'February' THEN 2
        WHEN 'March' THEN 3 
        WHEN 'April' THEN 4 
        WHEN 'May' THEN 5
        WHEN 'June' THEN 6 
        WHEN 'July' THEN 7 
        WHEN 'August' THEN 8
        WHEN 'September' THEN 9
        WHEN 'October' THEN 10 
        WHEN 'November' THEN 11 
        WHEN 'December' THEN 12 
    END PERSISTED

That way, you do the lookup faster, as integer. 这样,您可以更快地以整数形式进行查找。
Which will come in handy as the table fills and gets larger. 随着表的填充和变大,它将派上用场。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM