change sql parameter to date decimal

北战南征 提交于 2020-01-25 06:26:26

问题


I have a SQL command in crystal reports (its OK if your not familiar with crystal) and I need to convert a date parameter to a decimal (to match a column in the database.)

SELECT decimaldate FROM TABLE1 WHERE decimaldate = {?normaldate} 
--ex: 12/01/2011 needs to become 12012011

IF I use a CAST on the above query it doesn't work:

SELECT decimaldate FROM TABLE1 WHERE decimaldate =
 CAST(CAST{?normaldate} AS VARCHAR) AS DECIMAL)

回答1:


Try something like this.

 select CAST(replace(convert(varchar, getdate(), 101), '/', '') AS DECIMAL)

Or something like this where @normaldate is the search date.

SELECT decimaldate FROM TABLE1 WHERE decimaldate = CAST(replace(convert(varchar, @normaldate, 101), '/', '') AS DECIMAL)



回答2:


I suggest creating a formula (called something like @decimaldate) in formula to hold the equivalent numeric value of your date paramter - so it would be something like:

year({?normaldate})*10000 + month({?normaldate})*100 + day({?normaldate})

- then amend your selection criteria to select based on your new formula - like so:

SELECT decimaldate FROM TABLE1 WHERE decimaldate = {@decimaldate}



回答3:


I think VARCHAR_FORMAT() is actually what you're looking for:

SELECT decimaldate
  FROM table1
 WHERE decimaldate = VARCHAR_FORMAT(@NormalDate, 'MMDDYYY')

You may have to wrap @NormalDate with DATE() to cast it to a date type (it depends on your input format).




回答4:


This DB2 SQL function performs the date to MDY decimal conversion your query needs. Once it's created, your queries can compare a decimal column containing an MDY date to the output of UTIL.TO_DECIMAL_MDY( someValidDate )

CREATE OR REPLACE FUNCTION util.to_decimal_mdy(dateval DATE)
LANGUAGE SQL
RETURNS DECIMAL(9,0)
RETURN MONTH(dateval) * 10000000 + DAY(dateval) * 100000 + YEAR(dateval)
;


来源:https://stackoverflow.com/questions/8578252/change-sql-parameter-to-date-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!