简体   繁体   English

在SQL Server中将YYYYMM格式转换为YYYY-MM-DD

[英]Converting YYYYMM format to YYYY-MM-DD in SQL Server

I need to perform a query on a large table that has a datetime column that is indexed. 我需要对具有已索引的datetime列的大表执行查询。 We need to query the data for a range from a month (at a minimum) to multiple months. 我们需要查询一个月(至少)到多个月的数据。

This query would be executed from Cognos TM1 and the input would be a period like YYYYMM . 该查询将从Cognos TM1执行,输入将是类似YYYYMM My question is - how to convert the YYYYMM input to a format that can be used to query that table (with the index being used). 我的问题是-如何将YYYYMM输入转换为可用于查询该表(使用索引)的格式。

Let's say if the input is 假设输入是否为

  • From Date: '201312' 起始日期:“ 201312”
  • To Date: '201312' 迄今为止:“ 201312”

then, we need convert the same to 'between 01-12-2013 and 31-12-2013' in the query 然后,我们需要在查询中将其转换为“ 2013年12月12日至2013年12月31日之间”

Since we need this to be hooked up in Cognos TM1, so would not be able to write a procedure or declare variables (TM1 somehow does not like it). 由于我们需要将其连接到Cognos TM1中,因此将无法编写过程或声明变量(TM1某种程度上不喜欢它)。

Thanks in advance for your reply. 预先感谢您的答复。

Suppose you are getting this value of YYYYMM in a varchar variable @datefrom . 假设您在varchar变量@datefrom中获得了YYYYMM值。

You can do something like 你可以做类似的事情

DECLARE @DateFrom VARCHAR(6) = '201201';

-- Append '01' to any passed string and it will get all 
-- records starting from that month in that year

DECLARE @Date VARCHAR(8) = @DateFrom + '01'

-- in your query do something like 

SELECT * FROM TableName WHERE DateTimeColumn >= @Date

Passing Datetime in a ansi-standard format ie YYYYMMDD is a sargable expression and allows sql server to take advantage of indexes defined on that datetime column. 以ansi标准格式(即YYYYMMDD )传递Datetime是一个可表达的表达式,它允许sql server利用该datetime列上定义的索引。

here is an article written by Rob Farley about SARGable functions in SQL Server . 这是Rob Farley撰写的有关SARGable functions in SQL Server的文章。

I would do something like this: 我会做这样的事情:

create procedure dbo.getDataForMonth

  @yyyymm char(6) = null

as

  --
  -- use the current year/month if the year or month is invalid was omitted
  -- 
  set @yyyymm = case coalesce(@yyyymm,'')
                when '' then convert(char(6),current_timestamp,112)
                else         @yyyymm
                end

  --
  -- this should throw an exception if the date is invalid
  --
  declare @dtFrom date = convert(date,@yyyymm+'01') -- 1st of specified month
  declare @dtThru date = dateadd(month,1,@dtFrom)   -- 1st of next month

  --
  -- your Big Ugly Query Here
  --
  select *
  from dbo.some_table t
  where t.date_of_record >= @dtFrom
    and t.date_of_record < @dtThru

  --
  -- That's about all there is to it.
  --
  return 0
go

Try this... 尝试这个...

declare @startdate date,@endate date

select @startdate =convert(date,left('201312',4)+'-'+right('201312',2)+'-01')

select @endate= DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @startdate) + 1, 0))

select convert(date,@startdate,102) startdate,convert(date,@endate,102) endate

In the datasource of your TM1 Turbo Integrator process, you can use parameters in the SQL query. 在TM1 Turbo Integrator进程的数据源中,可以在SQL查询中使用参数。 Eg you could take this SQL query: 例如,您可以执行以下SQL查询:

SELECT Col1, Col2
FROM Table
WHERE Col1 = 'Green'
AND Col2 < 30

In TM1, to parameterise this, you would create two parameters eg P1 and P2 and put them in the query: 在TM1中,要对此进行参数化,您将创建两个参数,例如P1和P2并将其放入查询中:

SELECT Col1, Col2
FROM Table
WHERE Col1 = '?P1?'
AND Col2 < ?P2?

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

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