Hello all.
I'm trying to use a CASE statement to return the Month name from a month
number (1=Jan, 2=Feb, ect.) I have a field in my db that stores the date as
epoch time.
Here's the entire SQL, I get an "syntax error near =" when I run this:
SELECT
CONVERT(char(20), DATEADD(second, Create_Date + 3600 * - 6, 'Jan 1, 1970'),
100) AS Create_Date_Converted,
DATEPART (month, CONVERT(char(20), DATEADD(second, Create_Date + 3600 * - 6,
'Jan 1, 1970'), 100)) AS Create_Date_Converted_Month,
DATEPART (month, CONVERT(char(20), DATEADD(second, Create_Date + 3600 * - 6,
'Jan 1, 1970'), 100)) =
CASE
WHEN '1' THEN 'Jan'
WHEN '2' THEN 'Feb'
WHEN '3' THEN 'Mar'
END,
Create_Date,
Priority
FROM ARAdmin.Help_Desk
I'm a total newbie. Trying the CASE part of this query in different ways
hasn't resulted in any solutions. Any/all help is greatly apprecitated!lol, this is easy,, use the DateName() Function
Forget the case
Select DateName(month, getdate())
If you just want the abbreviation, take the Leftmost 3 Characters
Select Left(DateName(month, getdate()), 3)
"Drew" wrote:
> Hello all.
> I'm trying to use a CASE statement to return the Month name from a month
> number (1=Jan, 2=Feb, ect.) I have a field in my db that stores the date a
s
> epoch time.
> Here's the entire SQL, I get an "syntax error near =" when I run this:
> SELECT
> CONVERT(char(20), DATEADD(second, Create_Date + 3600 * - 6, 'Jan 1, 1970')
,
> 100) AS Create_Date_Converted,
> DATEPART (month, CONVERT(char(20), DATEADD(second, Create_Date + 3600 * -
6,
> 'Jan 1, 1970'), 100)) AS Create_Date_Converted_Month,
> DATEPART (month, CONVERT(char(20), DATEADD(second, Create_Date + 3600 * -
6,
> 'Jan 1, 1970'), 100)) =
> CASE
> WHEN '1' THEN 'Jan'
> WHEN '2' THEN 'Feb'
> WHEN '3' THEN 'Mar'
> END,
> Create_Date,
> Priority
> FROM ARAdmin.Help_Desk
> I'm a total newbie. Trying the CASE part of this query in different ways
> hasn't resulted in any solutions. Any/all help is greatly apprecitated!|||Thanks CBretana.
Your suggestion is to use DateName instead of DATEPART?
"CBretana" wrote:
> lol, this is easy,, use the DateName() Function
> Forget the case
> Select DateName(month, getdate())
> If you just want the abbreviation, take the Leftmost 3 Characters
> Select Left(DateName(month, getdate()), 3)
>
> "Drew" wrote:
>|||On a quick glance, you have a invalid SQL expression in your SELECT
statement. IOW, the statement you post forms a construct similar to:
SELECT <some_expression>,
<some_expression>,
<some_expression> = CASE expression, -- wrong
<other_column>
FROM <your_table>
The third column in the SELECT statement is invalid since it does not return
a valid SQL datatype. CASE expressions evaluates a list of conditions and
returns one of multiple possible result expressions. It can be used in any
SQL clause where a regular expression can be used or in the LHS or RHS of a
valid comparison operator. See the topc CASE in SQL Server Books Online for
more details.
Anith|||Yes, this function does exactly what you said you wanted,, it returns the
monthname (or any date part name) from a date,
"Drew" wrote:
> Thanks CBretana.
> Your suggestion is to use DateName instead of DATEPART?
> "CBretana" wrote:
>|||Thanks CBretana, works like a charm.
"CBretana" wrote:
> lol, this is easy,, use the DateName() Function
> Forget the case
> Select DateName(month, getdate())
> If you just want the abbreviation, take the Leftmost 3 Characters
> Select Left(DateName(month, getdate()), 3)
>
> "Drew" wrote:
>
No comments:
Post a Comment