Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

caseWhen.md

1,297 lines20.3 KB

1---
2id: caseWhen
3title: caseWhen
4---
5​
6## Call Signature
7​
8```ts
9function caseWhen<C1, V1>(condition1, value1): CaseWhenResult<[V1], false>;
10```
11​
12Defined in: [packages/db/src/query/builder/functions.ts:408](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L408)
13​
14Returns the value for the first matching condition, similar to SQL
15`CASE WHEN`.
16​
17Arguments are evaluated as condition/value pairs followed by an optional
18default value. Scalar branch values return a query expression and can be used
19in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
20`having`, and equality join operands. If no scalar branch matches and no
21default is provided, the result is `null`.
22​
23When a branch value is a projection object, `caseWhen` becomes a select-only
24projection value. Projection branches can include nested fields, ref spreads,
25and includes. If no projection branch matches and no default is provided, the
26result is `undefined`.
27​
28### Type Parameters
29​
30#### C1
31​
32`C1` *extends* `ExpressionLike`
33​
34#### V1
35​
36`V1` *extends* `CaseWhenValue`
37​
38### Parameters
39​
40#### condition1
41​
42`C1`
43​
44#### value1
45​
46`V1`
47​
48### Returns
49​
50`CaseWhenResult`\<\[`V1`\], `false`\>
51​
52### Examples
53​
54```ts
55caseWhen(gt(user.age, 18), `adult`, `minor`)
56```
57​
58```ts
59caseWhen(
60 gt(user.age, 65),
61 `senior`,
62 gt(user.age, 18),
63 `adult`,
64 `minor`,
66```
67​
68```ts
69caseWhen(gt(user.age, 18), {
70 ...user,
71 posts: q
72 .from({ post: postsCollection })
73 .where(({ post }) => eq(post.userId, user.id)),
74})
75```
76​
77## Call Signature
78​
79```ts
80function caseWhen<C1, V1, D>(
81 condition1,
82 value1,
83defaultValue): CaseWhenResult<[V1, D], true>;
84```
85​
86Defined in: [packages/db/src/query/builder/functions.ts:412](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L412)
87​
88Returns the value for the first matching condition, similar to SQL
89`CASE WHEN`.
90​
91Arguments are evaluated as condition/value pairs followed by an optional
92default value. Scalar branch values return a query expression and can be used
93in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
94`having`, and equality join operands. If no scalar branch matches and no
95default is provided, the result is `null`.
96​
97When a branch value is a projection object, `caseWhen` becomes a select-only
98projection value. Projection branches can include nested fields, ref spreads,
99and includes. If no projection branch matches and no default is provided, the
100result is `undefined`.
101​
102### Type Parameters
103​
104#### C1
105​
106`C1` *extends* `ExpressionLike`
107​
108#### V1
109​
110`V1` *extends* `CaseWhenValue`
111​
112#### D
113​
114`D` *extends* `CaseWhenValue`
115​
116### Parameters
117​
118#### condition1
119​
120`C1`
121​
122#### value1
123​
124`V1`
125​
126#### defaultValue
127​
128`D`
129​
130### Returns
131​
132`CaseWhenResult`\<\[`V1`, `D`\], `true`\>
133​
134### Examples
135​
136```ts
137caseWhen(gt(user.age, 18), `adult`, `minor`)
138```
139​
140```ts
141caseWhen(
142 gt(user.age, 65),
143 `senior`,
144 gt(user.age, 18),
145 `adult`,
146 `minor`,
148```
149​
150```ts
151caseWhen(gt(user.age, 18), {
152 ...user,
153 posts: q
154 .from({ post: postsCollection })
155 .where(({ post }) => eq(post.userId, user.id)),
156})
157```
158​
159## Call Signature
160​
161```ts
162function caseWhen<C1, V1, C2, V2>(
163 condition1,
164 value1,
165 condition2,
166value2): CaseWhenResult<[V1, V2], false>;
167```
168​
169Defined in: [packages/db/src/query/builder/functions.ts:417](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L417)
170​
171Returns the value for the first matching condition, similar to SQL
172`CASE WHEN`.
173​
174Arguments are evaluated as condition/value pairs followed by an optional
175default value. Scalar branch values return a query expression and can be used
176in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
177`having`, and equality join operands. If no scalar branch matches and no
178default is provided, the result is `null`.
179​
180When a branch value is a projection object, `caseWhen` becomes a select-only
181projection value. Projection branches can include nested fields, ref spreads,
182and includes. If no projection branch matches and no default is provided, the
183result is `undefined`.
184​
185### Type Parameters
186​
187#### C1
188​
189`C1` *extends* `ExpressionLike`
190​
191#### V1
192​
193`V1` *extends* `CaseWhenValue`
194​
195#### C2
196​
197`C2` *extends* `ExpressionLike`
198​
199#### V2
200​
201`V2` *extends* `CaseWhenValue`
202​
203### Parameters
204​
205#### condition1
206​
207`C1`
208​
209#### value1
210​
211`V1`
212​
213#### condition2
214​
215`C2`
216​
217#### value2
218​
219`V2`
220​
221### Returns
222​
223`CaseWhenResult`\<\[`V1`, `V2`\], `false`\>
224​
225### Examples
226​
227```ts
228caseWhen(gt(user.age, 18), `adult`, `minor`)
229```
230​
231```ts
232caseWhen(
233 gt(user.age, 65),
234 `senior`,
235 gt(user.age, 18),
236 `adult`,
237 `minor`,
239```
240​
241```ts
242caseWhen(gt(user.age, 18), {
243 ...user,
244 posts: q
245 .from({ post: postsCollection })
246 .where(({ post }) => eq(post.userId, user.id)),
247})
248```
249​
250## Call Signature
251​
252```ts
253function caseWhen<C1, V1, C2, V2, D>(
254 condition1,
255 value1,
256 condition2,
257 value2,
258defaultValue): CaseWhenResult<[V1, V2, D], true>;
259```
260​
261Defined in: [packages/db/src/query/builder/functions.ts:428](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L428)
262​
263Returns the value for the first matching condition, similar to SQL
264`CASE WHEN`.
265​
266Arguments are evaluated as condition/value pairs followed by an optional
267default value. Scalar branch values return a query expression and can be used
268in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
269`having`, and equality join operands. If no scalar branch matches and no
270default is provided, the result is `null`.
271​
272When a branch value is a projection object, `caseWhen` becomes a select-only
273projection value. Projection branches can include nested fields, ref spreads,
274and includes. If no projection branch matches and no default is provided, the
275result is `undefined`.
276​
277### Type Parameters
278​
279#### C1
280​
281`C1` *extends* `ExpressionLike`
282​
283#### V1
284​
285`V1` *extends* `CaseWhenValue`
286​
287#### C2
288​
289`C2` *extends* `ExpressionLike`
290​
291#### V2
292​
293`V2` *extends* `CaseWhenValue`
294​
295#### D
296​
297`D` *extends* `CaseWhenValue`
298​
299### Parameters
300​
301#### condition1
302​
303`C1`
304​
305#### value1
306​
307`V1`
308​
309#### condition2
310​
311`C2`
312​
313#### value2
314​
315`V2`
316​
317#### defaultValue
318​
319`D`
320​
321### Returns
322​
323`CaseWhenResult`\<\[`V1`, `V2`, `D`\], `true`\>
324​
325### Examples
326​
327```ts
328caseWhen(gt(user.age, 18), `adult`, `minor`)
329```
330​
331```ts
332caseWhen(
333 gt(user.age, 65),
334 `senior`,
335 gt(user.age, 18),
336 `adult`,
337 `minor`,
339```
340​
341```ts
342caseWhen(gt(user.age, 18), {
343 ...user,
344 posts: q
345 .from({ post: postsCollection })
346 .where(({ post }) => eq(post.userId, user.id)),
347})
348```
349​
350## Call Signature
351​
352```ts
353function caseWhen<C1, V1, C2, V2, C3, V3>(
354 condition1,
355 value1,
356 condition2,
357 value2,
358 condition3,
359value3): CaseWhenResult<[V1, V2, V3], false>;
360```
361​
362Defined in: [packages/db/src/query/builder/functions.ts:441](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L441)
363​
364Returns the value for the first matching condition, similar to SQL
365`CASE WHEN`.
366​
367Arguments are evaluated as condition/value pairs followed by an optional
368default value. Scalar branch values return a query expression and can be used
369in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
370`having`, and equality join operands. If no scalar branch matches and no
371default is provided, the result is `null`.
372​
373When a branch value is a projection object, `caseWhen` becomes a select-only
374projection value. Projection branches can include nested fields, ref spreads,
375and includes. If no projection branch matches and no default is provided, the
376result is `undefined`.
377​
378### Type Parameters
379​
380#### C1
381​
382`C1` *extends* `ExpressionLike`
383​
384#### V1
385​
386`V1` *extends* `CaseWhenValue`
387​
388#### C2
389​
390`C2` *extends* `ExpressionLike`
391​
392#### V2
393​
394`V2` *extends* `CaseWhenValue`
395​
396#### C3
397​
398`C3` *extends* `ExpressionLike`
399​
400#### V3
401​
402`V3` *extends* `CaseWhenValue`
403​
404### Parameters
405​
406#### condition1
407​
408`C1`
409​
410#### value1
411​
412`V1`
413​
414#### condition2
415​
416`C2`
417​
418#### value2
419​
420`V2`
421​
422#### condition3
423​
424`C3`
425​
426#### value3
427​
428`V3`
429​
430### Returns
431​
432`CaseWhenResult`\<\[`V1`, `V2`, `V3`\], `false`\>
433​
434### Examples
435​
436```ts
437caseWhen(gt(user.age, 18), `adult`, `minor`)
438```
439​
440```ts
441caseWhen(
442 gt(user.age, 65),
443 `senior`,
444 gt(user.age, 18),
445 `adult`,
446 `minor`,
448```
449​
450```ts
451caseWhen(gt(user.age, 18), {
452 ...user,
453 posts: q
454 .from({ post: postsCollection })
455 .where(({ post }) => eq(post.userId, user.id)),
456})
457```
458​
459## Call Signature
460​
461```ts
462function caseWhen<C1, V1, C2, V2, C3, V3, D>(
463 condition1,
464 value1,
465 condition2,
466 value2,
467 condition3,
468 value3,
469defaultValue): CaseWhenResult<[V1, V2, V3, D], true>;
470```
471​
472Defined in: [packages/db/src/query/builder/functions.ts:456](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L456)
473​
474Returns the value for the first matching condition, similar to SQL
475`CASE WHEN`.
476​
477Arguments are evaluated as condition/value pairs followed by an optional
478default value. Scalar branch values return a query expression and can be used
479in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
480`having`, and equality join operands. If no scalar branch matches and no
481default is provided, the result is `null`.
482​
483When a branch value is a projection object, `caseWhen` becomes a select-only
484projection value. Projection branches can include nested fields, ref spreads,
485and includes. If no projection branch matches and no default is provided, the
486result is `undefined`.
487​
488### Type Parameters
489​
490#### C1
491​
492`C1` *extends* `ExpressionLike`
493​
494#### V1
495​
496`V1` *extends* `CaseWhenValue`
497​
498#### C2
499​
500`C2` *extends* `ExpressionLike`
501​
502#### V2
503​
504`V2` *extends* `CaseWhenValue`
505​
506#### C3
507​
508`C3` *extends* `ExpressionLike`
509​
510#### V3
511​
512`V3` *extends* `CaseWhenValue`
513​
514#### D
515​
516`D` *extends* `CaseWhenValue`
517​
518### Parameters
519​
520#### condition1
521​
522`C1`
523​
524#### value1
525​
526`V1`
527​
528#### condition2
529​
530`C2`
531​
532#### value2
533​
534`V2`
535​
536#### condition3
537​
538`C3`
539​
540#### value3
541​
542`V3`
543​
544#### defaultValue
545​
546`D`
547​
548### Returns
549​
550`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `D`\], `true`\>
551​
552### Examples
553​
554```ts
555caseWhen(gt(user.age, 18), `adult`, `minor`)
556```
557​
558```ts
559caseWhen(
560 gt(user.age, 65),
561 `senior`,
562 gt(user.age, 18),
563 `adult`,
564 `minor`,
566```
567​
568```ts
569caseWhen(gt(user.age, 18), {
570 ...user,
571 posts: q
572 .from({ post: postsCollection })
573 .where(({ post }) => eq(post.userId, user.id)),
574})
575```
576​
577## Call Signature
578​
579```ts
580function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4>(
581 condition1,
582 value1,
583 condition2,
584 value2,
585 condition3,
586 value3,
587 condition4,
588value4): CaseWhenResult<[V1, V2, V3, V4], false>;
589```
590​
591Defined in: [packages/db/src/query/builder/functions.ts:473](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L473)
592​
593Returns the value for the first matching condition, similar to SQL
594`CASE WHEN`.
595​
596Arguments are evaluated as condition/value pairs followed by an optional
597default value. Scalar branch values return a query expression and can be used
598in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
599`having`, and equality join operands. If no scalar branch matches and no
600default is provided, the result is `null`.
601​
602When a branch value is a projection object, `caseWhen` becomes a select-only
603projection value. Projection branches can include nested fields, ref spreads,
604and includes. If no projection branch matches and no default is provided, the
605result is `undefined`.
606​
607### Type Parameters
608​
609#### C1
610​
611`C1` *extends* `ExpressionLike`
612​
613#### V1
614​
615`V1` *extends* `CaseWhenValue`
616​
617#### C2
618​
619`C2` *extends* `ExpressionLike`
620​
621#### V2
622​
623`V2` *extends* `CaseWhenValue`
624​
625#### C3
626​
627`C3` *extends* `ExpressionLike`
628​
629#### V3
630​
631`V3` *extends* `CaseWhenValue`
632​
633#### C4
634​
635`C4` *extends* `ExpressionLike`
636​
637#### V4
638​
639`V4` *extends* `CaseWhenValue`
640​
641### Parameters
642​
643#### condition1
644​
645`C1`
646​
647#### value1
648​
649`V1`
650​
651#### condition2
652​
653`C2`
654​
655#### value2
656​
657`V2`
658​
659#### condition3
660​
661`C3`
662​
663#### value3
664​
665`V3`
666​
667#### condition4
668​
669`C4`
670​
671#### value4
672​
673`V4`
674​
675### Returns
676​
677`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`\], `false`\>
678​
679### Examples
680​
681```ts
682caseWhen(gt(user.age, 18), `adult`, `minor`)
683```
684​
685```ts
686caseWhen(
687 gt(user.age, 65),
688 `senior`,
689 gt(user.age, 18),
690 `adult`,
691 `minor`,
693```
694​
695```ts
696caseWhen(gt(user.age, 18), {
697 ...user,
698 posts: q
699 .from({ post: postsCollection })
700 .where(({ post }) => eq(post.userId, user.id)),
701})
702```
703​
704## Call Signature
705​
706```ts
707function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, D>(
708 condition1,
709 value1,
710 condition2,
711 value2,
712 condition3,
713 value3,
714 condition4,
715 value4,
716defaultValue): CaseWhenResult<[V1, V2, V3, V4, D], true>;
717```
718​
719Defined in: [packages/db/src/query/builder/functions.ts:492](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L492)
720​
721Returns the value for the first matching condition, similar to SQL
722`CASE WHEN`.
723​
724Arguments are evaluated as condition/value pairs followed by an optional
725default value. Scalar branch values return a query expression and can be used
726in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
727`having`, and equality join operands. If no scalar branch matches and no
728default is provided, the result is `null`.
729​
730When a branch value is a projection object, `caseWhen` becomes a select-only
731projection value. Projection branches can include nested fields, ref spreads,
732and includes. If no projection branch matches and no default is provided, the
733result is `undefined`.
734​
735### Type Parameters
736​
737#### C1
738​
739`C1` *extends* `ExpressionLike`
740​
741#### V1
742​
743`V1` *extends* `CaseWhenValue`
744​
745#### C2
746​
747`C2` *extends* `ExpressionLike`
748​
749#### V2
750​
751`V2` *extends* `CaseWhenValue`
752​
753#### C3
754​
755`C3` *extends* `ExpressionLike`
756​
757#### V3
758​
759`V3` *extends* `CaseWhenValue`
760​
761#### C4
762​
763`C4` *extends* `ExpressionLike`
764​
765#### V4
766​
767`V4` *extends* `CaseWhenValue`
768​
769#### D
770​
771`D` *extends* `CaseWhenValue`
772​
773### Parameters
774​
775#### condition1
776​
777`C1`
778​
779#### value1
780​
781`V1`
782​
783#### condition2
784​
785`C2`
786​
787#### value2
788​
789`V2`
790​
791#### condition3
792​
793`C3`
794​
795#### value3
796​
797`V3`
798​
799#### condition4
800​
801`C4`
802​
803#### value4
804​
805`V4`
806​
807#### defaultValue
808​
809`D`
810​
811### Returns
812​
813`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `D`\], `true`\>
814​
815### Examples
816​
817```ts
818caseWhen(gt(user.age, 18), `adult`, `minor`)
819```
820​
821```ts
822caseWhen(
823 gt(user.age, 65),
824 `senior`,
825 gt(user.age, 18),
826 `adult`,
827 `minor`,
829```
830​
831```ts
832caseWhen(gt(user.age, 18), {
833 ...user,
834 posts: q
835 .from({ post: postsCollection })
836 .where(({ post }) => eq(post.userId, user.id)),
837})
838```
839​
840## Call Signature
841​
842```ts
843function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
844 condition1,
845 value1,
846 condition2,
847 value2,
848 condition3,
849 value3,
850 condition4,
851 value4,
852 condition5,
853value5): CaseWhenResult<[V1, V2, V3, V4, V5], false>;
854```
855​
856Defined in: [packages/db/src/query/builder/functions.ts:513](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L513)
857​
858Returns the value for the first matching condition, similar to SQL
859`CASE WHEN`.
860​
861Arguments are evaluated as condition/value pairs followed by an optional
862default value. Scalar branch values return a query expression and can be used
863in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
864`having`, and equality join operands. If no scalar branch matches and no
865default is provided, the result is `null`.
866​
867When a branch value is a projection object, `caseWhen` becomes a select-only
868projection value. Projection branches can include nested fields, ref spreads,
869and includes. If no projection branch matches and no default is provided, the
870result is `undefined`.
871​
872### Type Parameters
873​
874#### C1
875​
876`C1` *extends* `ExpressionLike`
877​
878#### V1
879​
880`V1` *extends* `CaseWhenValue`
881​
882#### C2
883​
884`C2` *extends* `ExpressionLike`
885​
886#### V2
887​
888`V2` *extends* `CaseWhenValue`
889​
890#### C3
891​
892`C3` *extends* `ExpressionLike`
893​
894#### V3
895​
896`V3` *extends* `CaseWhenValue`
897​
898#### C4
899​
900`C4` *extends* `ExpressionLike`
901​
902#### V4
903​
904`V4` *extends* `CaseWhenValue`
905​
906#### C5
907​
908`C5` *extends* `ExpressionLike`
909​
910#### V5
911​
912`V5` *extends* `CaseWhenValue`
913​
914### Parameters
915​
916#### condition1
917​
918`C1`
919​
920#### value1
921​
922`V1`
923​
924#### condition2
925​
926`C2`
927​
928#### value2
929​
930`V2`
931​
932#### condition3
933​
934`C3`
935​
936#### value3
937​
938`V3`
939​
940#### condition4
941​
942`C4`
943​
944#### value4
945​
946`V4`
947​
948#### condition5
949​
950`C5`
951​
952#### value5
953​
954`V5`
955​
956### Returns
957​
958`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`\], `false`\>
959​
960### Examples
961​
962```ts
963caseWhen(gt(user.age, 18), `adult`, `minor`)
964```
965​
966```ts
967caseWhen(
968 gt(user.age, 65),
969 `senior`,
970 gt(user.age, 18),
971 `adult`,
972 `minor`,
974```
975​
976```ts
977caseWhen(gt(user.age, 18), {
978 ...user,
979 posts: q
980 .from({ post: postsCollection })
981 .where(({ post }) => eq(post.userId, user.id)),
982})
983```
984​
985## Call Signature
986​
987```ts
988function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5, D>(
989 condition1,
990 value1,
991 condition2,
992 value2,
993 condition3,
994 value3,
995 condition4,
996 value4,
997 condition5,
998 value5,
999defaultValue): CaseWhenResult<[V1, V2, V3, V4, V5, D], true>;
1000```
1001​
1002Defined in: [packages/db/src/query/builder/functions.ts:536](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L536)
1003​
1004Returns the value for the first matching condition, similar to SQL
1005`CASE WHEN`.
1006​
1007Arguments are evaluated as condition/value pairs followed by an optional
1008default value. Scalar branch values return a query expression and can be used
1009in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1010`having`, and equality join operands. If no scalar branch matches and no
1011default is provided, the result is `null`.
1012​
1013When a branch value is a projection object, `caseWhen` becomes a select-only
1014projection value. Projection branches can include nested fields, ref spreads,
1015and includes. If no projection branch matches and no default is provided, the
1016result is `undefined`.
1017​
1018### Type Parameters
1019​
1020#### C1
1021​
1022`C1` *extends* `ExpressionLike`
1023​
1024#### V1
1025​
1026`V1` *extends* `CaseWhenValue`
1027​
1028#### C2
1029​
1030`C2` *extends* `ExpressionLike`
1031​
1032#### V2
1033​
1034`V2` *extends* `CaseWhenValue`
1035​
1036#### C3
1037​
1038`C3` *extends* `ExpressionLike`
1039​
1040#### V3
1041​
1042`V3` *extends* `CaseWhenValue`
1043​
1044#### C4
1045​
1046`C4` *extends* `ExpressionLike`
1047​
1048#### V4
1049​
1050`V4` *extends* `CaseWhenValue`
1051​
1052#### C5
1053​
1054`C5` *extends* `ExpressionLike`
1055​
1056#### V5
1057​
1058`V5` *extends* `CaseWhenValue`
1059​
1060#### D
1061​
1062`D` *extends* `CaseWhenValue`
1063​
1064### Parameters
1065​
1066#### condition1
1067​
1068`C1`
1069​
1070#### value1
1071​
1072`V1`
1073​
1074#### condition2
1075​
1076`C2`
1077​
1078#### value2
1079​
1080`V2`
1081​
1082#### condition3
1083​
1084`C3`
1085​
1086#### value3
1087​
1088`V3`
1089​
1090#### condition4
1091​
1092`C4`
1093​
1094#### value4
1095​
1096`V4`
1097​
1098#### condition5
1099​
1100`C5`
1101​
1102#### value5
1103​
1104`V5`
1105​
1106#### defaultValue
1107​
1108`D`
1109​
1110### Returns
1111​
1112`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`, `D`\], `true`\>
1113​
1114### Examples
1115​
1116```ts
1117caseWhen(gt(user.age, 18), `adult`, `minor`)
1118```
1119​
1120```ts
1121caseWhen(
1122 gt(user.age, 65),
1123 `senior`,
1124 gt(user.age, 18),
1125 `adult`,
1126 `minor`,
1128```
1129​
1130```ts
1131caseWhen(gt(user.age, 18), {
1132 ...user,
1133 posts: q
1134 .from({ post: postsCollection })
1135 .where(({ post }) => eq(post.userId, user.id)),
1137```
1138​
1139## Call Signature
1140​
1141```ts
1142function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
1143 condition1,
1144 value1,
1145 condition2,
1146 value2,
1147 condition3,
1148 value3,
1149 condition4,
1150 value4,
1151 condition5,
1152 value5,
1153 condition6,
1154 value6, ...
1155 rest): any;
1156```
1157​
1158Defined in: [packages/db/src/query/builder/functions.ts:561](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L561)
1159​
1160Returns the value for the first matching condition, similar to SQL
1161`CASE WHEN`.
1162​
1163Arguments are evaluated as condition/value pairs followed by an optional
1164default value. Scalar branch values return a query expression and can be used
1165in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1166`having`, and equality join operands. If no scalar branch matches and no
1167default is provided, the result is `null`.
1168​
1169When a branch value is a projection object, `caseWhen` becomes a select-only
1170projection value. Projection branches can include nested fields, ref spreads,
1171and includes. If no projection branch matches and no default is provided, the
1172result is `undefined`.
1173​
1174### Type Parameters
1175​
1176#### C1
1177​
1178`C1` *extends* `ExpressionLike`
1179​
1180#### V1
1181​
1182`V1` *extends* `CaseWhenValue`
1183​
1184#### C2
1185​
1186`C2` *extends* `ExpressionLike`
1187​
1188#### V2
1189​
1190`V2` *extends* `CaseWhenValue`
1191​
1192#### C3
1193​
1194`C3` *extends* `ExpressionLike`
1195​
1196#### V3
1197​
1198`V3` *extends* `CaseWhenValue`
1199​
1200#### C4
1201​
1202`C4` *extends* `ExpressionLike`
1203​
1204#### V4
1205​
1206`V4` *extends* `CaseWhenValue`
1207​
1208#### C5
1209​
1210`C5` *extends* `ExpressionLike`
1211​
1212#### V5
1213​
1214`V5` *extends* `CaseWhenValue`
1215​
1216### Parameters
1217​
1218#### condition1
1219​
1220`C1`
1221​
1222#### value1
1223​
1224`V1`
1225​
1226#### condition2
1227​
1228`C2`
1229​
1230#### value2
1231​
1232`V2`
1233​
1234#### condition3
1235​
1236`C3`
1237​
1238#### value3
1239​
1240`V3`
1241​
1242#### condition4
1243​
1244`C4`
1245​
1246#### value4
1247​
1248`V4`
1249​
1250#### condition5
1251​
1252`C5`
1253​
1254#### value5
1255​
1256`V5`
1257​
1258#### condition6
1259​
1260`ExpressionLike`
1261​
1262#### value6
1263​
1264`CaseWhenValue`
1265​
1266#### rest
1267​
1268...`CaseWhenValue`[]
1269​
1270### Returns
1271​
1272`any`
1273​
1274### Examples
1275​
1276```ts
1277caseWhen(gt(user.age, 18), `adult`, `minor`)
1278```
1279​
1280```ts
1281caseWhen(
1282 gt(user.age, 65),
1283 `senior`,
1284 gt(user.age, 18),
1285 `adult`,
1286 `minor`,
1288```
1289​
1290```ts
1291caseWhen(gt(user.age, 18), {
1292 ...user,
1293 posts: q
1294 .from({ post: postsCollection })
1295 .where(({ post }) => eq(post.userId, user.id)),
1297```