case

Postgres nested if in case query

為{幸葍}努か 提交于 2019-12-03 13:30:24
Could you tell my why the following isnt working in postgres sql?: See updated code below UPDATE: I expect the query to return "0.30" as float. This construct is only for testing purposes, i have some complex querys which depend on this conditional structure... BUt i dont know how to fix it.. Result is: ERROR: syntax error at or near "1" LINE 4: if 1=1 then UPDATE: This construction appears in a function... so I want to do following: CREATE FUNCTION f_test(myvalue integer) RETURNS float AS $$ BEGIN select ( case (select '1') when '1' then if 1=1 then 0.30::float else 0.50::float end else 1.00:

Case Statements and Pattern Matching

心不动则不痛 提交于 2019-12-03 13:28:32
I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.: Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it. fun all_except_option(str : string, lst : string list) = case lst of [] => NONE | x::xs => case same_string(x, str) of true => SOME xs | false =

Determine data type of a column in SQLite

不羁的心 提交于 2019-12-03 12:51:32
I'm working on an Android App where the user has different options for sorting the displayed data that comes from the database. Currently my orderBy string that I pass to Androids query() method looks like this: "LOWER("+columnName+") ASC" The problem with this is that if the data type in the column specified by columnName is integer, calling LOWER() on it will cause it to be sorted alphabetically, i.e. based only on the leftmost digit, which of course doesn't make any sense for numeric data. Hence I only want to apply LOWER() if the data type of the column is not integer. What I have in mind

T-SQL Where Clause Case Statement Optimization (optional parameters to StoredProc)

烂漫一生 提交于 2019-12-03 11:32:58
问题 I've been battling this one for a while now. I have a stored proc that takes in 3 parameters that are used to filter. If a specific value is passed in, I want to filter on that. If -1 is passed in, give me all. I've tried it the following two ways: First way: SELECT field1, field2...etc FROM my_view WHERE parm1 = CASE WHEN @PARM1= -1 THEN parm1 ELSE @PARM1 END AND parm2 = CASE WHEN @PARM2 = -1 THEN parm2 ELSE @PARM2 END AND parm3 = CASE WHEN @PARM3 = -1 THEN parm3 ELSE @PARM3 END Second Way:

MySQL update conditions in one query (UPDATE, SET & CASE)

烂漫一生 提交于 2019-12-03 11:21:24
I'm trying to update a set of records ( boolean fields ) in a single query if possible. The input is coming from paginated radio controls, so a given POST will have the page's worth of IDs with a true or false value. I was trying to go this direction: UPDATE my_table SET field = CASE WHEN id IN (/* true ids */) THEN TRUE WHEN id IN (/* false ids */) THEN FALSE END But this resulted in the "true id" rows being updated to true , and ALL other rows were updated to false . I assume I've made some gross syntactical error, or perhaps that I'm approaching this incorrectly. Any thoughts on a solution?

Why “final static int” can be used as a switch's case constant but not “final static <your enum>”

我们两清 提交于 2019-12-03 10:49:07
Why is this int switch valid: public class Foo { private final static int ONE = 1; private final static int TWO = 2; public static void main(String[] args) { int value = 1; switch (value) { case ONE: break; case TWO: break; } } } While this enum switch is not: import java.lang.annotation.RetentionPolicy; public class Foo { private final static RetentionPolicy RT = RetentionPolicy.RUNTIME; private final static RetentionPolicy SRC = RetentionPolicy.SOURCE; public static void main(String[] args) { RetentionPolicy value = RetentionPolicy.RUNTIME; switch (value) { case RT: break; case SRC: break; }

SWITCH with LIKE inside SELECT query in MySQL

时光怂恿深爱的人放手 提交于 2019-12-03 09:29:59
I have this Tags table CREATE TABLE IF NOT EXISTS `Tags` ( `id_tag` int(10) unsigned NOT NULL auto_increment, `tag` varchar(255) default NULL, PRIMARY KEY (`id_tag`), UNIQUE KEY `tag` (`tag`), KEY `id_tag` (`id_tag`), KEY `tag_2` (`tag`), KEY `tag_3` (`tag`), KEY `tag_4` (`tag`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2937 ; INSERT INTO `Tags` (`id_tag`, `tag`) VALUES (1816, '(class'), (2642, 'class\r\n\r\nâ?¬35'), (1906, 'class\r\nif'), (1398, 'class'), (2436, 'class)'), (1973, 'class:\n1.'), (2791, 'classes'), (1325, 'New'), (2185, 'pack'), (1905, 'packed'), (1389, 'WebClass');

Using || in Case switch in Rails

天涯浪子 提交于 2019-12-03 09:15:55
问题 I have a partial that I want to display in a layout only when certain pages use that layout. I've set @page_title for all my pages and thought I could use something like this: <% case @page_title when "Log in" || "Forgot Your Password" || "Create a New Password" %><%= render :partial => "common/hello-world" -%><% end -%> But, the include is only happening on the page titled "Log in" and not the other pages. Are || statements like this not allowed on Case switches? Is there a different way to

CASE statement in SQLite query

陌路散爱 提交于 2019-12-03 06:28:39
问题 Why this query doesn't work? :( I tried to replace nested IF statement "...SET lkey = IF(lkey >= 11, lkey - 5, IF(lkey > 5, lkey + 2,lkey))" UPDATE pages SET lkey = CASE lkey WHEN lkey >= 11 THEN lkey - 5 ELSE CASE lkey WHEN lkey > 5 THEN lkey + 2 ELSE lkey END END, rkey = CASE lkey WHEN lkey >= 11 THEN rkey - 5 ELSE CASE rkey WHEN rkey < 11 THEN rkey + 2 ELSE rkey END END WHERE rkey > 5 AND lkey < 12; 回答1: The syntax is wrong in this clause (and similar ones) CASE lkey WHEN lkey > 5 THEN

SQL changing a value to upper or lower case

混江龙づ霸主 提交于 2019-12-03 06:28:06
问题 How do you make a field in a sql select statement all upper or lower case? Example: select firstname from Person How do I make firstname always return upper case and likewise always return lower case? 回答1: SELECT UPPER(firstname) FROM Person SELECT LOWER(firstname) FROM Person 回答2: LCASE or UCASE respectively. Example: SELECT UCASE(MyColumn) AS Upper, LCASE(MyColumn) AS Lower FROM MyTable 回答3: SQL SERVER 2005: print upper('hello'); print lower('HELLO'); 回答4: You can use LOWER function and